Displaying Excerpts in WordPress

After using a bunch of different writing platforms, I’ve finally decided to move back to WordPress.  While I’m not a fan of its bloated complexity, the ability to configure it the way I want without resorting to extensive re-writes is great.

Case in point – the default themes in WordPress display the full content of each post on the various aggregation pages.  This is not terrible, but I prefer to only show excerpts.  This lets visitors more quickly scan the articles and find the one they want and removes a lot of the duplicate content on the site.

So yesterday I decided to adjust my site so that any page that was not a specific post page would only show the a snippet or summary of the post instead of the entire post.  I knew there had to be a way because by default the search function displayed only post snippets.

Sure enough, a quick search told me that I needed to replace a call to:

<?php the_content( ); ?>

(which returned the full content of a post) with a call to:

<?php the_excerpt(); ?>

which returned just an excerpt with a link to the full page.

A little more searching led me to the content.php file in the theme, where I found the following conditional (with some bits removed):

  <?php if ( is_search() ) : // Only display Excerpts for Search ?>
    <div class="entry-summary">
    <?php the_excerpt(); ?>
  </div><!-- .entry-summary -->  <?php else : ?>

So this was checking if the page was a search page, and if so, displaying an excerpt of the content instead of the entire content.  Jackpot!

Now all I needed was to adjust that conditional so that it would display the excerpt for ANY page that displayed multiple posts.  Searching for the is_search() method led me to the Conditional Tags page on the WordPress Codex.

WordPress provides a bunch of different checks to determine what type of page is being displayed.  In my case, the interesting check is the method is_singular() which does exactly what it sounds like – it returns true if a single post is being displayed and false otherwise.


Now all I needed to do was edit content.php to only display the content when a single post is being displayed.  So I replaced the line from above

<?php if ( is_search() ) : // Only display Excerpts for Search ?>

with a call to the is_singular() method (making sure that I check that the post is NOT singular before displaying the excerpt):

<?php if ( !is_singular() ) : // Display Excerpts for all pages with multiple posts ?>

That’s all there is to it!