Replace [...] with a Post Link

Posted on Friday, December 2nd, 2011 at 4:48 pm in

Excerpts (teasers) can be shown on WordPress through two methods:

  1. Using <?php the_content();?> in your theme template file(s) and then inserting the <!-- more--> into your post at your desired cut-off point.
  2. Using <?php the_excerpt();?&gt; in your theme template file(s) and allowing WordPress to generate a teaser from the first 55 words of the post’s content — without the need to place a <!-- more--> tag in the post.

Option 1 automatically creates a nice “read more” link at the bottom of each teaser that allows readers to go straight to the full, single, post but it does mean that you have to remember to enter that pesky <!-- more--> tag into your posts before you publish them.

Option 2 is fully automatic but lacks the nice “read more” link. So how can you have all of the benefits of the automatic excerpt but also provide a link to the the single post?

Simply edit your theme’s functions.php file and add the following function after the opening <?php tag.

function my_excerpt_link() {
	return ' <a class="more-link" href="'. get_permalink() . '">' .  printf(__( 'Read $s'), get_the_title() ) . '</a>';
}
add_filter( 'excerpt_more', 'my_excerpt_link' );

The above code will generate an automatic link to the single post in the format:

Read Replace [...] with a Post Link

Now you get the benefits of both options.

You might also be interested in
Tags:

WordPress Support Services

Top