WordPress Excerpt 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,
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, Monthly archives, Author archives etc
The most commonly used method for adding Excerpt to the 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 suites 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 provide a Filter Hook for to alter the Read More text. The filter hook simply returns the desired text and replace it with the default. The Filter hook name is excerpt_more. Lets have a look on 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 link to the Read More >>.
Changing the size of Excerpt
Now, it’s time to change the size of the excerpt. WordPress provides an another Filter Hook to change the size of 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; }
This entry was posted in Uncategorized on February 11, 2015 by Rndexperts.