Get updates

Scott Q&A: How to get WordPress home to link to a single previous post?

WordPress is a handy blogging platform, especially when it’s time for customization. By default the home page shows a number of full posts and then links to another page with multiple full posts. For some this works fine, but I’d prefer not to contend with duplicate content issues and instead let older posts speak for themselves on their own unique pages.

Question: How can the home of a WordPress blog be made to forgo links to pages full of multiple previous posts, and just use a link to the single previous post instead?

Answer: All it takes is counting the number of posts the loop has displayed (to know when it’s at the end – don’t throw another conditional check on have_posts in the loop or you’re likely to create an endless loop, and yes, I found that out the hard way), then employing a hack to display a link to a single post.

The solution presented here is not the most elegant method possible, but it does the job. First we need a means of counting/controlling the number of posts spit out in the loop. We could use the value set in the options panel but there are advantages to exercising finer control than that global variable allows. Justin Blanton shows an easy way to limit the number of posts with WordPress which does just fine for our purposes.

After the desired number of posts have been displayed, a little is_single hack posted by Kafkaesqui allows us to display a previous link to a single article by changing the home page context.

Put them together and the code may look something like this (in your theme’s index.php):

<?php if (have_posts()) : ?>

  <?php $ctr = 0;
  $index_show_most = 5; ?>

  <?php while (have_posts()) : the_post(); ?>

    <?php if ($ctr >= $index_show_most) { break; }
    else
    { ?>

      (do the post stuff here)

    <?php }
    $ctr++;
    if($ctr == $index_show_most)
    {
      $wp_query->is_single = true; ?>
      <?php previous_post_link(); ?>
      <?php $wp_query->is_single = false;
    } ?>

  <?php endwhile; ?>

The variable $index_show_most simply sets the number of posts to show on the home page; this is effectively limited to a maximum of the value set for Show at Most in Options for Reading. Setting this to a low number while the option is set higher allows a few posts on home with many more displayed in archives and categories.

2 Responses to “Q&A: How to get WordPress home to link to a single previous post?”

  1. Thanks for the tip – it worked perfectly.

  2. Glad you found it useful Ryan!

Leave a Reply


Fatal error: Uncaught Error: Call to undefined function show_subscription_checkbox() in /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/comments.php:90 Stack trace: #0 /home/dmetro/3dmdesign.com/wp-includes/comment-template.php(1471): require() #1 /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/single.php(65): comments_template() #2 /home/dmetro/3dmdesign.com/wp-includes/template-loader.php(74): include('/home/dmetro/3d...') #3 /home/dmetro/3dmdesign.com/wp-blog-header.php(19): require_once('/home/dmetro/3d...') #4 /home/dmetro/3dmdesign.com/index.php(17): require('/home/dmetro/3d...') #5 {main} thrown in /home/dmetro/3dmdesign.com/wp-content/themes/hex_columns/comments.php on line 90