WordPress shows 55 words in excerpts by default and ends them with […]. Two filters let you change both.

Change the excerpt length

function lotgrafix_excerpt_length( $length ) {
    return 25;
}
add_filter( 'excerpt_length', 'lotgrafix_excerpt_length', 999 );

The high priority number makes sure your filter runs after the theme and the plugins.

Change the ending of the excerpt

function lotgrafix_excerpt_more( $more ) {
    return '…';
}
add_filter( 'excerpt_more', 'lotgrafix_excerpt_more' );

Add a Read More link

function lotgrafix_excerpt_read_more( $more ) {
    return '&hellip; <a class="lotgrafix-more" href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Read more', 'lotgrafix' ) . '</a>';
}
add_filter( 'excerpt_more', 'lotgrafix_excerpt_read_more' );

Trim any text to a number of words

Use this when you want a custom length in one specific place, without changing the global setting.

echo wp_trim_words( strip_shortcodes( get_the_content() ), 30, '&hellip;' );

Enable excerpts on pages

function lotgrafix_page_excerpts() {
    add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', 'lotgrafix_page_excerpts' );

Notes

  • If an author writes a manual excerpt, excerpt_length does not apply to it.
  • For Chinese, Japanese and Korean sites, WordPress counts characters instead of words.
«
»