Tutorials

How To Display Full First Post Then Excerpts In WordPress Loop

Pinterest LinkedIn Tumblr

As the title states, I have been trying to customize WordPress loop and messing with my theme’s template to get the main blog page to show the entire latest post and the rest as only excerpts. Digging through the WordPress.org forums I found just enough information to really make a mess of my blog. I also noticed that the question was asked a few times over there and well… let’s just say the response was a bit condescending. Like the answer was so simple that if you had to ask you didn’t deserve to even be on the forum.

It did take a little trial and error on my part to implement it and the results are worthy. If you are going to do this I suggest that you to add your own excerpts when writing posts to control what is shown under the title of the post on the main blog page.

The file that needs to be changed is index.php/front-page.php/home.php (depending on your theme).

Display full first post then excerpts

Setting up a WordPress site that is impressive yet informative at the first glance can be quite tricky to achieve. The most effective way used by website owners in this regard is the display of full first post and then excerpts for the rest of the posts.

The beginners might not want to get into the coding details but trust us, doing this does not involve any prior coding knowledge.

The process involves replacing codes in the loop. For the starters, the loop is a PHP code used by WordPress to display posts.

It is easy and here is how:

Displaying the number of posts

You would want the viewers to see the number of posts you have to get the initial idea about your website. For that to happen, you need to customize your loop code.

Look for the following code in your WordPress loop:

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

Use this code instead:

<?php if (have_posts()) : ?>
<?php $postcount = 0; // Initialize the post counter ?>
<?php while (have_posts()) : the_post(); //start the loop ?>
<?php $postcount++; //add 1 to the post counter ?>

Displaying First Full Post then Excerpts

Once you are done with the above step which however is not a prerequisite but is highly recommended, proceeds to the following:

Look for this code in the loop:

<?php the_content('Read the rest of this entry &raquo;'); ?>

Replace the_content with the following code:

<?php if ($postcount == 1) : // if this is the first post ?>
<?php the_content('Read the rest of this article ->'); //Show the full post ?>
<?php else : //if this is NOT the first post ?>
<?php the_excerpt(); ?>

<?php endif; //end of the check for first post - other posts?>

Note: The counter will show a value of 1 for displaying the full post otherwise it will only show the excerpt.

Final Word

Once you are done replacing the codes, your website is good to go!

Editorial Staff

Editorial Staff at WPArena is a team of WordPress experts led by Jazib Zaman. Page maintained by Jazib Zaman.

Write A Comment

Pin It