How to Make Excerpts & Read More Links for Twenty Twelve

Posted on by .

Like many, I’ve used Twenty Twelve as my base theme and have been working on customising it.  There are a couple of things I often find annoying about WordPress themes, though, such as lack of excerpts and read more links by default (for some anyway, but not all).  Plugins are available, however it’s extra overhead for something that can be (and should be) done within the theme.  In this post, I’ll describe for you how to do that.

If you’re basing your theme upon another one, it’s best to set up a child theme, so that you can keep the main theme up-to-date without overwriting your custom changes.  This is a case where I do use a plugin (as I don’t always have the chance to use FTP or a file manager to do it myself), but it’s one that doesn’t take up unnecessary resources for page loads anyway.  I was using One Click Child Theme, but looking at the support forum there are reports that it is broken (at least with some themes), but there are a few other plugins in WordPress.org’s plugin repository.  Otherwise, do it manually. The following instructions are specific to Twenty Twelve.  Copy “content.php” from the parent to the child theme’s folder.  Edit it (Appearance > Editor in WordPress admin) and look for line 35 (it may vary depending upon your version of the theme and if you have made any edits), which contains the following:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>

And replace it with:

<?php if ( is_search() || is_archive() || is_author() || is_tag() || is_home() ) : // Only display Excerpts for Search ?>

This will make WordPress show excerpts on tag, author, archive (category) and search pages as well as the home page.  If you don’t want an excerpt shown on one of those particular pages then just remove the relevant entry and its corresponding “||” (which means OR), e.g. if you want full posts on the home page then amend it so it looks like the following:

<?php if ( is_search() || is_archive() || is_author() || is_tag() ) : // Only display Excerpts for Search ?>

Save the changes and refresh your site. Now it should be using excerpts, but of course “read more” links are missing. This is easy to resolve by adding the following to your theme’s “functions.php” file (copy it from the parent theme if your child theme does not have one).

function excerpt_read_more() {
return ' [...] <a class="read-more" href="'. get_permalink() . '">Read the full post &gt;</a>';
}
add_filter('excerpt_more', 'excerpt_read_more');

Your can customise the text and CSS class to what suits your needs.  I use the greater than (“&gt;” in the code, which HTML renders as “>”) symbol, but you can change it to anything you want.

Leave a Reply

Your email address will not be published. Required fields are marked *