Over the last week or so, I’ve been talking a lot about using excerpts and More Tags on your blog, as well as customizing the Read More link displayed by the More Tag. Today I want to cover how to make your manual excerpts display on your home page, with a read more link, for every single post, so you get the benefit of both a More Tag and an Excerpt, without have to set up both every time you publish new content. I love things that save me time.
As I’ve previously discussed, there are a number of good reasons to limit the amount of each post that displays on your home page, and both the More Tag and manual excerpts offer their own advantages. Modifying your theme like we are doing today will allow you to gain the benefits of both.
Customizing Your Theme’s Code To Display The Excerpt
The goal here is to use conditional tags to basically tell your theme “If this post has an excerpt, display it. If it doesn’t have one, just show the entire post instead.” The reason we want to do it this way is because if we simply replace the
the_excerpt(), whenever you have a post without an excerpt, the default behavior is to simply display the first 55 words of the post. That’s not what you want.
Now, your theme may already contain the conditional tags we’ll be using, but for now we’ll proceed as if your blog calls just
the_content(), which means you’ll be looking for something similar to this:
<?php the_content('Read on...'); ?>
Your code may (and probably does) look different depending on how your theme is set up, but this template tag basically tells WordPress to display the content of a given post. Either way, you’ll notice that the text your blog displays when you place a More Tag into a post is controlled by this tag. What we are going to do is replace this tag with the conditional tags I mentioned above, like this:
<?php if (function_exists('has_excerpt') && has_excerpt()) the_excerpt(); else the_content('Read on...'); ?>
the_content() tag is still here, on the line that begins with “else.”
For some themes, the conditional tags shown above are already mostly in place, with one exception. If your theme has this structure but still isn’t displaying excerpts, the second line of the code shown above probably looks like this:
if (function_exists('has_excerpt') && has_excerpt()) the_content();
The only difference is that it ends in
the_content(), rather than
the_excerpt(). Changing that will do the trick!
Hey! Where’s My “Read More” Link?
Now that your blog is displaying excerpts, you probably want to add a link that will make it clear to your readers that there is more to read, and entice them to do so (like the More Tag does). All you need to do is add an anchor tag that includes the post’s permalink, like so:
<a href="<?php the_permalink(); ?>">Continue Reading</a>
Replace the “Continue Reading” with whatever you want to customize the text that is displayed. Placing this code just after the conditional tags we edited a moment ago will place the link at the bottom of each excerpt. If you want, you can even include the title of the post in the excerpt by calling the
get_title() tag, like this:
<a href="<?php the_permalink(); ?>">Continue Reading "<?php the_title(); ?>"</a>
This tag will automatically grab the title of the post in question. The quotation marks around it are just so the title will appear in quotations.
Once you’ve got your link displaying the right text, you can fancy it up a bit by creating a CSS class for it to control color, size, alignment, etc.
The only “drawback” – and I use quotes here because I don’t really think it’s a big one – is that this link is not controlled by the conditional tags we entered earlier. That means the “Read More” link you just created will show up on the home page of your blog for every post, even if that post has no excerpt and the entire content is displayed. If you’re anything like me and plan to add an excerpt to every single post you write, then that’s not a big deal.
UPDATE: There is a small piece of additional code you can add in to create a link that will only display for posts with excerpts – it just took me a little longer than it should have to realize it.