Modifying the WordPress excerpt length and truncation.

There are sometimes when you want to modify the WordPress excerpt to make it longer, shorter or to modify the truncation characters.

This is actually really easy to do. All we need to do is add a simple function that modifies the WordPress filter.

How do we do it?

We just need to add the following code to our functions.php file.

Modify the excerpt length.


<?php
if ( ! function_exists( 'uncoverwp_excerpt_length' ) ) :
    /**
     * Excerpt function to allow us to modify
     * the_excerpt length for display purposes
     */
    function uncoverwp_excerpt_length( $length )
    {
        // the length we want our excerpt
        return 50;
    }
    add_filter( 'excerpt_length', 'uncoverwp_excerpt_length', 999 );
endif;

Modify the excerpt truncation.


<?php
if ( ! function_exists( 'uncoverwp_excerpt_more' ) ) :
    /**
     * Excerpt more function to allow us to modify
     * the text displayed for the_excerpt text truncation
     */
    function uncoverwp_excerpt_more( $more )
    {
        // the characters we would like to display when the excerpt is truncated
        return '...';
    }
    add_filter( 'excerpt_more', 'uncoverwp_excerpt_more' );
endif;

UncoverWP

Want us to email you occasionally with our latest courses, news & updates?

We use cookies to ensure that we give you the best possible experience on our website. If you continue to use this site we will assume that you are happy with it. OK Privacy Policy