Skip to content



How To Create Conditional “Read More” Links For Manual Excerpts

Manual WordPress ExcerptA little while back I posted an article on using manual excerpts with read more links on the home page of your blog. In that article I mentioned that there was only one drawback to the method I was describing: the “read more” link we created wasn’t controlled by the same “if statement” that displayed the excerpt, so it would show up even on posts that didn’t actually have an excerpt (and were therefore already displaying their full content).

Certainly not the end of the world, but definitely unnecessary. Today we fix that problem by adding some code to your theme that will display the link only if it’s actually needed.

Now if you read the post in question, you’ll probably remember that we called the excerpt like this:

<?php
   if (function_exists('has_excerpt') && has_excerpt()) the_excerpt();
   else the_content('Read on...');
?>

Which worked pretty well. The adjustment we’re going to make today, so that our custom read more link is conditional, is a pretty simple one (which is why I feel silly for not thinking of it while I was writing the original post ;) ). All we need to do is add another conditional statement after the excerpt is displayed.

<?php
 if (function_exists('has_excerpt') && has_excerpt()) { ?>
<a href="<?php the_permalink() ?>" title="Continue Reading <?php the_title_attribute(); ?>...">Read the rest of <?php the_title(); ?> &raquo;</a>
<?php  } ?>

All we’re doing here is repeating the check to see if an excerpt is present (just like above). Only this time, instead of actually displaying the excerpt, we break out of PHP for a moment to add our read more link.

You’ll notice I got a little fancier with the link this time around so that the title of the post in question is included in the title tag (using the_title_attribute()) as well as in the actual anchor text (using the_title()).

Don’t forget to open up a final pair of PHP tags to close out the curly braces that contain the if-statement’s actions.

There you go! You now have a link that not only contains the actual name of the post that it goes to, but will only display when it’s needed. This also solves the problem of using the occasional More Tag for a post… with this code added into the mix, you won’t need to worry about having two links show up!

Enjoy!


7 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. pinky says

    Hi,
    I seem to have worked it out – thanks for the this code, just wasn’t working through your articles and inserting [b]all[/b] the code I needed to at once. My remaining task is to work out how to insert thumbnail images with the excerpts and I’m thinking there may be a plugin for that…

  2. Chad says

    Hey Pinky,

    Glad you got it worked out!

    You should be able to include an image in your excerpt by uploading the image, and then including the code for it at the beginning of the excerpt. :)

  3. Sandy says

    I’m still puzzling over how to automatically work out if there is more content after the excerpt if you aren’t using the MORE tags and just showing an automatic excerpt.

  4. Chad says

    @Sandy

    Not sure I understand what you’re asking… could you clarify a bit?

  5. Nick says

    Chad, I’m just barely starting to try to wrap my head around PHP and I know this is a dumb question but can you please explain:
    “Don’t forget to open up a final pair of PHP tags to close out the curly braces that contain the if-statement’s actions.”

    I copied the code exactly but I have to add braces somewhere correct? If I have to put in braces, why don’t you show the exact code to copy? Or am I confused?

    Thanks for the help!

  6. Chad says

    @Nick

    Not a dumb question at all! The curly brace I mention there is included in the code example – I only mention it again because it’s the kind of thing that can be easy to forget (especially if you’re new to PHP) so if anyone were typing the code manually, rather than copy/pasting, I didn’t want them to skip it. :)

  7. John says

    Just what I needed. This should be built into WordPress by default.
    Thanks a lot!



Some HTML is OK

or, reply to this post via trackback.