Many WordPress sites take advantage of post excerpts to display snippets of posts on the home page or archive pages of their site, often followed by a “Read More” or “Read the full article…” link. There are actually a number of ways that an excerpt can be generated, and your theme files need to deal with a couple scenarios to get them to appear the way that you like. Let’s take a look at each one.

the_content() vs. the_excerpt()

Your theme could use one of two functions to generate an excerpt for posts. Many themes will use the_content() to output an excerpt. According to the codex, the_content() will show an excerpt up to the <!--more--> quicktag on non-single/non-permalink posts, such as archives, categories, front page and searches. On single posts, the more quicktag is ignored, and it will show the full content of the post. This allows the writer to select the excerpt from the beginning of the post by manually placing the <!--more--> quicktag. The downside of this approach is that the entire post will show up on your archive page if the writer didn’t use the more quicktag.

If you don’t want to rely on your writers selecting an excerpt by entering the more quicktag, replace the_content() with the_excerpt(). (More on the_excerpt from the Codexthe_excerpt will automatically generate a teaser for the post based on the first words in the post. The number of words automatically selected for the excerpt is controlled by the excerpt_length filter.

Whether you are using the_content() or the_excerpt() a writer can override these excerpts by entering a manual excerpt in the excerpt meta box when editing a post. When a manual excerpt has been entered, it is used instead of the text before the more quicktag, in the case of the_content, or instead of the auto-trimmed word count in the case of the_excerpt(). Custom post types must enable “excerpt” when they are configured with register_post_type for this meta box to be enabled.

Adding a “Read More” hyperlink to post excerpts

Many websites like to add a “Read More” link to the end of their post snippets, often with a hyperlink to the full article. There are multiple ways that you can customize a “Read More” link which depend on whether you are using the_content(), the_excerpt() or manual excerpts.

If you are using the_content() in your templates in conjunction with the <!--more--> quicktag, you’ll can control the “Read More” link text using the two parameters that the_content() supports.

<?php the_content( $more_link_text , $strip_teaser ); ?>

The $more_link_text parameter allows you to customize the link text to whatever you like. $strip_teaser takes a boolean where you can turn on or off the teaser article text before the “Read More” link.

If you are using the_excerpt() in your templates and would like a “Read More” link to your full article, you’ll need to explicitly enable it by using the excerpt_more filter like below. Be sure to pay attention to the Codex instructions if you are using this filter in a Child Theme.

// Replaces the excerpt "more" text by a link
function new_excerpt_more($more) {
       global $post;
	return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

The last situation is when you have a manually-crafted excerpt that was entered in the excerpt meta box. Neither of the above approaches will generate a “Read More” link, as the_excerpt() simply outputs the text entered in the meta box as-is. In this case you’ll need to filter get_the_excerpt() to generate a “Read More” link, first checking to see if a manual excerpt exists. Once again, pay attention to the Codex instructions if you are working in a Child Theme.

//display read more link when there is a manually entered exceprt
function excerpt_read_more($excerpt) {
    if (has_excerpt() && !is_attachment()) {
        $excerpt .= '&hellip; <a href="' . get_permalink() . '">Read More</a>';
    }
    return $excerpt;
}
add_filter('get_the_excerpt', 'excerpt_read_more');

Relevant Codex articles

Latest Developer Blog Articles

Job Listings on GeekWork

Find more jobs on GeekWork. Employers, post a job here.