Categories
Technology

WordPress Excerpt

WordPress Excerpt A WordPress Post excerpt is a short summary of your post. The excerpt has two main use. It replaces the full content of post in RSS Feed. It can be used for Search results,

The WordPress Post excerpt is a short summary of your post. The excerpt has two main uses.

  • It replaces the full content of the post in RSS Feed.
  • It can be used for Search results, Monthly archives, Author archives, etc

The most commonly used method for adding Excerpt to a WordPress post is to use a function in the template.

 the_excerpt(); 

Mostly in a WordPress setup, we have noticed [..] as a symbol of Read More which sometimes doesn’t suit the theme or design of the website. Here we are going to show you some interesting methods to play with WordPress Excerpt.

Changing the Read More text

WordPress provides a Filter Hook to alter the Read More text. The filter hook simply returns the desired text and replaces it with the default. The Filter hook name is excerpt_more. Let’s have a look at the example code to alter the text.

 add_filter( 'excerpt_more', 'rnd_read_more'   );
 function rnd_read_more() {  
   return 'Read More »'; 
} 

Please this code inside the function.php of your active theme or inside the main file of your plugin and we are done with altering the Read More text.

Adding link to Read More

I hope you are still with me and still checking the post. We have just altered the Read More text but the text is simply a text, not a link. Which may confuse the users. Now, what if you wanted to redirect the user once they click on Read More >>.

add_filter( 'excerpt_more', 'rnd_read_more'   ); 
function rnd_read_more() { 
return    '<small><a href="'.get_the_permalink(get_the_ID()).'">Read More »</a></small>';
} 

The above code will add a link to the Read More >>.

Changing the size of Excerpt

Now, it’s time to change the size of the excerpt. WordPress provides another Filter Hook to change the size of the excerpt. The hook named excerpt_length. Below is the code to change the size with this hook.

add_filter( 'excerpt_length', 'rnd_excerpt_limit', 999 ); 
function rnd_excerpt_limit() {  
return 20;
}