Using WordPress Excerpts

Doing more with PHP the Excerpt. One of the things that I’ve noticed over time is that the excerpt functionality of WordPress is often neglected. For a long time I thought that perhaps the idea of having to type of an excerpt just triggered the lazy person in most of us and we didn’t feel like doing it. I’m personally a big fan of excerpts, even if I don’t write them up on this blog as much as I do on other blogs, so I started to take a closer look at why excerpts might be so neglected.

I very quickly came to develop a theory that perhaps it’s because users don’t think they do anything – and in many cases they’re absolutely correct. There are a startling number of WordPress themes that don’t include a built in ability to show the excerpt anywhere, so it’s no wonder people see little use in typing up extra words that aren’t even shown. Much to my surprise, I realized that even the 2 themes that ship with WordPress (the default and the classic) don’t automatically display excerpts. Very odd.

In my way of thinking, the MAIN page on a typical blog should display posts with the following conditions applied (granted, I’ve created some VERY custom themes that you’d never guess are blog driven, so keep in mind that this is for the ‘typical blog’ style) :

  1. Check to see if there is an excerpt, if so, show the excerpt only.
  2. If no excerpt is present, check to see if there is a ‘more’ tag when showing the post and use a link to continue reading.
  3. If no excerpt or more tag is found, then display the whole post.

So how do you do that with WordPress in your templates? It’s pretty easy.

I’ll give you instructions using the WordPress default template (the one based on Kubrick at the time of this writing).

  1. In the Administration backend, go to Presentation -> Theme Editor
  2. Select ‘WordPress default’ from the drop-down list, then click the select button.
  3. On the right hand side, locate the file link that says ‘Main Index Template’ and click on it.

Now that you are in the correct section, locate the following piece of code:
<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

You are going to replace that entire piece of code with the following:

<div class="entry">
<?php if (!empty($post->post_excerpt)) : ?>
<?php the_excerpt(); ?>
<?php else : ?>
<?php the_content('Read the rest of this entry &raquo;'); ?>
<?php endif; ?>
</div>

That will create the functionality to do exactly what I outlined above, however, if you’d like to also add a link to the post underneath the excerpt (if one exists) so that it looks similar to the way a ‘more’ tagged post will look, you can do that as well. To create a link to the post under the excerpt, simply go back to this line:

<?php the_excerpt(); ?>

And add a line similar to the following underneath that line:

<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read the full post &raquo;</a></p>

That line is just normal WordPress post link, so you can modify that to suit your needs design needs.

The finish product code display various types of posts like this, depending on whether they have excerpts, more tags, or none:

Various Views of Excerpts.

Of course, this example uses the WordPress default template, but the same code can be used on most any template if you find the index.php or main index page and the spot where <?php the_content(); ?> is in the code.

Have fun!

~Nicole

11 Replies to “Using WordPress Excerpts”

  1. Thanks for the helpful, clear explanation. I’ve been looking for plugins to achieve what I want and a slightly modified version of this is all I need. Simple. Thanks…

Comments are closed.