WordPress

Custom exerpts with excerpt_length and excerpt_more

Limit excerpt length to 50 words

Put the following code in functions.php:

function themify_custom_excerpt_length( $length ) {
   return 50;
}
add_filter( 'excerpt_length', 'themify_custom_excerpt_length', 999 );

Use 999 as the priority to ensure that the function runs after the default WordPress filter, otherwise it would override what is set here.

Adding a Read More link at the end of the excerpt

To do this, put the following code in functions.php:

function custom_excerpt_more($more) {
   return '<a href="'. get_permalink($post->ID) . '">Read More</a>';
}
add_filter('excerpt_more', 'custom_excerpt_more');

The results should look like this:

Real world example from swiftsrbija.rs

Adding a few dots at the end of the excerpt

In our functions.php

function new_excerpt_more( $more ) {
    return '.....';
}
add_filter('excerpt_more', 'new_excerpt_more');

We should get this:

Real world example from swiftsrbija.rs website


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow