On the homepage of a WordPress site I was working on, I wanted to display the most recent post completely (not just the excerpt) while the next 9 showing the excerpt only. I also wanted to include a post image and other additional meta data for better usability and also to make the latest article stand out from the others.
My initial approach was to use the is_home() conditional tag from the WordPress Codex. Problem with this method was whenever you browsed through the previous 10 entries using the navigation links on the home page the next 10 articles would be displayed in the same manor as the first 10; The top article would show as a full post while the next 9 would show excerpt only, even tho the top article is not always the latest article published.
So what I wanted to achieve was, when a user lands on my homepage they would see the latest article’s full content and in reverse chronological order and 9 more posts showing the excerpt only. If the user clicked the navigation links (previous 10 entries) I wanted the next 10 entries to show their excerpt’s only without a full post at the top.
After thinking about it for a couple minutes there was a simple solution by adding a simple counter to my while loop and evaluating what number post (of 10) was being returned and returning 2 different results.
If the count is at 0 (1st time through the loop) and we are on the the homepage then return the_content rather then just the_excerpt:
<?php while (have_posts()) : the_post(); ?>
<?php static $ctr = 0;
if ($ctr == "5" && is_home()) { break; }
else { ?>
<div class="post">
<h4><a href="<?php the_permalink(); ?>" title="Permalink to "<?php the_title(); ?>""><?php the_title(); ?></a></h4>
<p class="meta"><?php the_time('F jS, Y') ?> | <?php the_category(', ') ?><span class="alignright bubble"><?php comments_popup_link('0', '1', '%'); ?></span></p>
<div class="entry">
<?php if ($ctr == "0" && is_home() && !is_paged()) { the_content("Continue Reading..."); } else if { ?>
<?php the_excerpt('Read the rest of this entry »'); } ?>
</div>
</div>
<?php $ctr++; } ?>
<?php endwhile; ?>
This is a very basic example of what you can do with this, you can also use this to display the number of posts to display within your loop, rather than the default “10″, but be creative and and experiment with this to make your latest WordPress post stand out from the previous entries anywhere you might be listing your posts.
Tags: WordPress