• I’m not an expert on coding. I’d like to have only my most recent post (singular) on my front/home page. However, I don’t want to simply adjust the reading settings because then when an archive search is processed, only one result will be produced and I don’t want that.

    Basically I’m asking is there any code for the home.php specifically that I could change to make this happen?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I use this loop code for index.php to show all of the latest post and only excerpts of all later posts. If you change the While PHP statement then you can limit the number of posts to as many as you want.

    <?php if (have_posts()) : ?>
    <br />
    		<?php while (have_posts()) : the_post();
    		$numPosts = $numPosts + 1; ?>
    
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<h2><a style="text-align:left; text-decoration: none;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    				<small><b><?php the_time('F jS, Y') ?> by <?php the_author() ?></b></small>
    
    				<div class="entry">
    <?php
    if($numPosts != 1) {
    the_excerpt(); ?><span class="read_more"><a href="<?php the_permalink(); ?>">Read More >></a></span> <?php
     } else {
    	the_content('Read the rest of this entry >>');
     } ?>
    				</div>
    
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    			</div>
    <p></p>
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    <?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php // include (TEMPLATEPATH . "/searchform.php"); ?>
    
    <?php endif; ?>

    Before your loop add the template tag, query_posts():

    <?php query_posts($query_string . '&showposts=1'); ?>

    Also, the Custom Query String Reloaded plugin can help you with that.
    https://moshublog.com/2007/10/30/custom-query-string-reloaded-for-wordpress-23-with-tag-support/

    Hi Michael,

    I have the same problem.

    If I use query_posts function, paging not worked properly. When I click on older entries, it shows 404 page.

    Any other way of doing this?

    Thanks
    Kapil Jain

    Might need:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string . '&showposts=1&paged='.$paged);
    ?>

    Hi Michael,

    Thanks for quick reply.

    I have already try this, but not worked.

    The reason of the Error is, if the number of pages according to the wordpress blog setting (from reading section of wp-admin) is less than the current page (using query_posts function) is shows 404 error message.

    For example,

    if I have 7 posts in my blog and set Blog pages show at most to 5. It means my blog have 2 pages.

    and if I display 2 post in my home page using query_posts function, my home page shows 4 pages.

    It works fine for first 2 pages but shows 404 error in 3rd and 4th page.

    Any idea how to resolve this issue.

    Thanks
    Kapil Jain

    Try the Custom Query String Reloaded plugin to see if that helps.

    this is a problem with the url-rewrite code for permalinks, it will work normally if permalinks is turned off, but breaks with permalinks on.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘restrict the number of posts on front page’ is closed to new replies.