Displaying the whole content of the latest post and excerpt for the rest, in WordPress

Knowledge Base on June 30th, 2010 No Comments

This post was originally published at LionVineyard.com on 2009/02/27

Recently I was struggling how to display the posts on the main page of one of the blogs I am administering. Displaying the whole content of posts was making the main page too long, displaying only the summary was making the main page kind of dull as it was missing the pictures.

The problem:

Display the whole content of the first post and summary for the rest.

The post display on the main page is controlled by the infamous The Loop. In order to achieve this The Loop has to be tempered.

I did some search but could not find something that would solve my problem so I decided to go with my instinct which is greatly influenced by my Fortran background.

A typical The Loop that displays the whole content looks like this:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//commands to display title, author, date, here
<?php the_content(__('Read more'));?>
<?php endwhile; else: ?>
// some message here if no posts are found
<?php endif; ?>

The the_content() function can be replaced by the_excerpt() function to display the summary.

The solution:
Define a numeric variable within the if loop, and increment it within the while loop. In this case the variable is x. The new The Loop looks like this:

<?php if (have_posts()) : $x=0; while (have_posts()) : $x++; the_post(); ?>
//commands to display title, author, date, here
<?php if ($x==1) : the_content(__('Read more')); else: the_excerpt(); endif;?>
<?php endwhile; else: ?>
// some message here if no posts are found
<?php endif; ?>

So the above implementation should display the whole content of the first post and the summary of the rest.
If you have any suggestions that are more PHP friendly or even WordPress friendly in achieving this please leave your comments below…

No Responses to “Displaying the whole content of the latest post and excerpt for the rest, in WordPress”

Leave a Reply

We stop spam together!

RSS Feed
on facebook
@twitter
footer