Excerpt versus Content for Archives

Most theme developers have slowly moved away from using the_excerpt to display content on index, archive and search pages. I definitely have.

Let’s go over quickly how the_excerpt works:

  1. If an excerpt is explicitly defined, it will be displayed (including HTML).
  2. If an excerpt isn’t defined, the post content will be used, but stripped of HTML and truncated to 55 words by default. An indication that is has been truncated also displays “[…]”.

If a post consists entirely of a YouTube embed, a gallery, a single image, or a Tweet embed, “the_excerpt” will strip all that out- and thus nothing will be displayed. This makes for a very weird looking archive page if the_except is used.

The alternative is to use the_content. This allows the full content to show. But the content can also be truncated on archives by using more tag.

This is what I’ve been using in most of my new themes as it is the most flexible.

Alternative

Recently I’ve been wondering if there is a better way. This solution might be overly complex, but it at least guarantees that archives have content to display and gives users a lot of choice over how it’s done.

Here’s how it works:

  1. If an except is explicitly set, use that.
  2. If a “more” tag is used, display the_content.
  3. If the post is shorter than 200 words, display the_content.
  4. If none of those conditions are met, use the_excerpt.

Translated into code:

<?php
if ( has_excerpt() ) :
	the_excerpt();
elseif ( @strpos( $post->post_content, '<!--more-->') ) :
	the_content();
elseif ( str_word_count( $post->post_content ) < 200 ) :
	the_content();
else:
	the_excerpt();
endif;
?>

I’d be curious for any feedback or thoughts on this approach.

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Cratejoy. Find me on twitter @devinsays.

8 Responses

  1. I like it. I might wrap it up in a myslug_the_excerpt() function and use it from now on. Excerpts are such a pain. I wish the edit post screen pushed awareness of manual excerpts more.

  2. I just noticed today that Twenty Fifteen doesn’t use the_excerpt. It show the full post content unless “more” is used. I think that’s nice and simple. Automatic excerpts confuse users because links and breaks are stripped. But crafting HTML for manual excerpts is not intuitive for users.

    I’ve been doing something similar to you by first checking if the more tag is present and if so then use the_content. Otherwise, the_excerpt is used. It lets people use “more”, a manual excerpt or an automatic excerpt. I’ve been doing this for more than a year and it seems intuitive enough for people.

    It’s not perfect though because people get confused about automatic excerpts stripping out their HTML and even though few have requested it, they cannot show the full content. I really like the simplicity of ditching the_excerpt altogether and letting users opt for the “more” tag for blog themes but that’s probably not an option for every custom post type.

  3. Nice approach. I’ve been offering a choice in the customizer between the full content and the excerpt for some time now. If a user picks the full content and a post has an excerpt, the theme will show the excerpt instead. It’s worked in that people can have whatever they want, but this would let me get rid of an option :)

  4. Just like Bill, I have an option in the Customizer to show content or excerpt, but I think you’re on to something here. I’ll try to tweak it and see how it works in my case, because less options is always a good thing indeed. ;)

    Nate, I also think the manual excerpts should be more obvious. The thumbnail is what captures the eye, but a well written intro text is what can really push you to click that snippet of content. Not to mention it’s a great tool when creating custom post types and just need a single place to add some extra text, like a subtitle, description, etc.

  5. I use the same approach for my premium themes. But instead of hard-coding for what to display, I add an option for what will be displayed:

    – Post excerpt
    – Post content before more tag
    – Post content (with an additional option for number of words)

    And to do all this logic, I write a custom function for that. It’s very similar to what you have in the post, but a little bit more complicated.

Leave a Reply