excerpt_length not working

The are two quick ways to display custom excerpt lengths in your theme using wp_trim_words. Remember, if you use the_excerpt(), your excerpt length will always be a maximum of 55, never more. If you use the_content() on the other hand, you can specify an excerpt length of more than 55 words.

Use the following to display your excerpt. Remember to replace get_the_excerpt with get_the_content if you need an excerpt of more than 55, and replace <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a> with any excerpt ending you need. My ending display a “read more” text with the name of the post.

function pietergoosen_custom_excerpts($limit) {
    return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a>');
}

Now just use echo pietergoosen_custom_excerpts($limit); anywhere in your templates where you need to display excerpts. Just change $limit to the actual amount of words, for example echo pietergoosen_custom_excerpts(45); to display 45 words

EDIT

Have a look at my answer on a custom excerpt as well

Leave a Comment