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:
- If an excerpt is explicitly defined, it will be displayed (including HTML).
- 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.
This is what I’ve been using in most of my new themes as it is the most flexible.
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:
- If an except is explicitly set, use that.
- If a “more” tag is used, display the_content.
- If the post is shorter than 200 words, display the_content.
- 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.