WordPress Read More And Excerpt Customization Tips

If you are a blogger on wordpress or an avid blog reader, you would appreciate the importance of post excerpts in wordpress which provide you with a small abstract of the post text ( typically the first few lines of the post ). The excerpt is followed by a hyper link which says something like “Read More” or “Continue Reading”. As a blogger, you would not expect a reader to go to each individual post and have a look at the same for information. Rather, the reader might like a small overview list of post excerpts; from where the decision on which post to further read-on would be made.

Few of the themes have this facility in built and you might not be required to add it explicitly. However, in few others, you would either be required to modify the code a bit or add a new piece altogether. I am currently using Arthemia wordpress theme and had to customize it a bit in order to get the desired results. I would share with you the changes done by me which would be useful for you as well to customize your own wordpress blog theme.

Customizing The Excerpt

For customizing the excerpt, a new function was added in functions.php called custom_trim_excerpt which overrides the default wordpress excerpt function. Refer the code below.


remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');

function custom_trim_excerpt($text) {
global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $pattern = '/()/';
                $replacements='';
                $text = preg_replace($pattern, $replacements, $text);
               $text = strip_tags($text, '

');

  	         $excerpt_length = apply_filters('excerpt_length', 60);
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words) > $excerpt_length) {
			array_pop($words);
			array_push($words, '...');
			$text = implode(' ', $words);
		}
	}
	return $text;
}

If you notice above, we do a a few important things here

  • The add_filter maps the excerpt function to our own custom function
  • Inside our function, we strip any img ( image ) tags from the post content.
  • We remove any paragraph tags in turn removing all formatting/bullets and making the post content as plain text.
  • Then we extract the first 60 words from the post to display in the excerpt ( this can be modified as per requirement )

Now if we call the excerpt function from within any wordpress file( refer below ), we would get the formatted text corresponding to this function

<?php the_excerpt(); ?>

Adding the Read More

For adding the <Read More> or <Continue Reading>, you just need to add one line after the excerpt function mentioned above typically in index.php, search.php and archive.php. Just look for the following code in the above mentioned files.

<?php the_excerpt(); ?>

Just below this, add the following

<a href="<?php the_permalink() ?>" rel="bookmark"><b>Read More...</b></a>

As you can notice, this will add a hyperlink with anchor text as <Read More> and pointing to the post’s actual URL corresponding to the permalink. As an overall effect, you will see a post excerpt and just below it the <Read More> link. So with a couple of changes, you would get a nice looking desired functionality. Refer snapshot below to see how the <Read More> looks on the home page of Inficone for this very post.

If you liked this post, do share it with your friends. If you think there are better ways to do this, do update us in the comments. You can always submit any useful tools/web services/tips to us.





Inficone runs on the Genesis Framework

Genesis Framework
Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!
Follow On Twitter
Follow On Facebook
Follow Using RSS
Follow Using Email
Tweet This Post
Share Post On Facebook
Digg This Post
Stumble Upon

Speak Your Mind

*

This blog is kept spam free by WP-SpamFree.