Subscribe: RSS | Email | Widget

Photo Credits

Using WordPress Excerpts

Nicole Hernandez on February 29th, 2008

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

15 Responses to “Using WordPress Excerpts”

  1. Thank you for such a clear explanation!

  2. thanks much, it is great article

  3. Great explanation, i was looking for a conditional to check if excerpt exist!

  4. Thanks, your guide worked!

  5. Very well written and clear. This helped with my first edit of a theme, and it worked perfectly.

  6. This guide is very usefull, My friend and I found this and saved us some time :)

  7. 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…

  8. Thank you, this is exactly what l was looking for!

    Works great!

  9. Thanks. This is usefull..

  10. Thanks for this tip, I too was looking for a code snippet to check whether an excerpt exists!

  11. Perfect! Exactly what I was looking for thank you :)

Trackbacks/Pingbacks

  1. Useful wordpress plugins and template tweaks | welcomebrand
  2. silicon guru » Blog Archive » Wordpress tips
  3. Conditional tags in Wordpress « ふ
  4. Razee » Blog Archive » How to Use the Excerpt as a Description

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>