WordPress.org

Ready to get started?Download WordPress

Forums

Static page with dynamic content (8 posts)

  1. tjobbe
    Member
    Posted 5 years ago #

    I have a page set as my home page and I would like the home page to also display my latest posts from a certain category, is this possible?

    I have searched quite a bit and tried a few things but can't quite figure this out, can anyone give me some advice as to how this would be done please?

  2. Ben Sutcliffe
    Member
    Posted 5 years ago #

    You need to crate a page template for your page and then add the following code, adjusting for your chosen category.

    The code then would be this (adapted):

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     <!-- We only want category 3, for eg, so: -->
     <?php if (in_category('3')) { ?>
    
     <div class="post">
    
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
      <small><?php the_time('F jS, Y'); ?></small>
    
      <div class="entry">
        <?php the_content(); ?>
      </div>
    
      <p class="postmetadata">Posted in <?php the_category(', '); ?>
    
     </div> <!-- closes the first div box -->
    
     <?php }; endwhile; else: ?>
     Sorry, no posts were found in <?php single_cat_title(); ?>.
    
     <?php endif; ?>
  3. tjobbe
    Member
    Posted 5 years ago #

    bsutcliffe - thanks for the quick reply!

    I've created a page specifically for the home page and I have set this to be the home page as a static page.

    I then created a template called main.php and uploaded it to the theme folder, and then I made sure that the page in wordpress used the main.php template.

    This seems to work fine, but it now does not show my usual sidebar but one that is designed for posts, see below:

    This is sidebar.php, and displays content NOT destined for the home page, but for posts pages, so all the conditional stuff i there that says "if this is a page or if this is the home page" is ignored

    <div id="sideBar">
    			<?php /* If this is the frontpage */ if ( is_front_page() ) { ?>
    			<img src="intro.jpg" alt="Cooking Parties!" />
    			<?php } ?>
    			<?php /* If this is a page */ if (is_page()) { ?>
     			<div class="colourOne">
    				<p><a href="/2009/parties/">Book a Pudding Pie cookery party..</a></p>
    			</div>
    			<div class="colourTwo">
    				<p><a href="/2009/courses/">Learn to cook with home based courses..</a></p>
    			</div>
    			<div class="colourOne">
    				<p><a href="/2009/ingredients/">See the ingredients we use in our food..</a></p>
    			</div>
    			<div class="colourTwo">
    				<p><a href="/2009/shop/">Shop for Pudding Pie vouchers &amp; stuff</a></p>
    			</div>
    			<div class="colourOne">
    				<p><a href="/2009/category/recipes/">Browse our free recipes for you to try..</a></p>
    			</div>
    			<div class="colourTwo">
    				<p><a href="/2009//contact-us/">Contact Us</a></p>
    			</div>
    			<?php /* If this is the frontpage */ if ( is_front_page() ) { ?>
    			<div id="twitter_div">
    				<h2 class="sidebar-title">Twitter Updates</h2>
    				<ul id="twitter_update_list"></ul>
    				<a href="http://twitter.com/pudding_pie" id="twitter-link" style="display:block;text-align:right;">follow me on Twitter</a>
    			</div>
    			<?php } ?>
    			<?php } else { ?>
    			<h2>Categories;</h2>
    			<ul id="categories">
    			<?php wp_list_categories('title_li='); ?>
    			</ul>
    			<?php } ?>
    	</div>

    What am i doing wrong?

  4. Ben Sutcliffe
    Member
    Posted 5 years ago #

    So do you want to call a different sidebar for your homepage, rather than the default one?

    You call the sidebar from the page template (as you would call the header and the footer) so you can always call another sidebar: <?php include(TEMPLATEPATH . "/sidebar2.php"); ?>

  5. tjobbe
    Member
    Posted 5 years ago #

    I'll try that, thank you!

  6. tjobbe
    Member
    Posted 5 years ago #

    Perfect, works brilliantly, but the one thing I'd like it to do is mimic where I have inserted the <!--more--> tag, is this possible? At the moment it shows the whole post - not including the image for some reason - and doesn't get cut off as it does on the category page , is this possible?

  7. tjobbe
    Member
    Posted 5 years ago #

    Here is the code I am using, what would I need to change to get it to show the content up to the more tag?

    <h3>Latest free recipe</h3>
    <?php $temp_query = $wp_query; ?>
    <?php query_posts('category_name=recipes&amp;showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
  8. Ben Sutcliffe
    Member
    Posted 5 years ago #

    Lift the code you have in archive.php and paste it over <?php the_excerpt(); ?> directly. I'm not sure why you're call to the_excerpt() isn't pulling this - are you sure you've uploaded you're most recent change correctly (and cleared your cache)?

Topic Closed

This topic has been closed to new replies.

About this Topic