Creating Excerpts in WordPress – Tags, Functions, & Notes

WordPress is a beautiful rig. It’s the world’s most popular and relied-upon CMS. Here’s how you can activate, modify, and place excerpts in WordPress.

By default WordPress sets excerpts (short previews of a blog or custom post type) to 55 characters. The character limit is a good one that works in most situations. However, it is possible to modify the character count. Here’s how.

How To Change Default Excerpt Character Count

To modify the character count, you’ll need to access the functions.php file of your particular theme. The best way to do that is to access it through the server files, likely through cPanel.

Once you’ve access the functions.php file and have opened it up to be edited, you can use the snippet below to set the character count you desire.

/**
 * Filter the except length to 20 words.
 *
 * @param int $length Excerpt length.
 * @return int (Maybe) modified excerpt length.
 */
function wpdocs_custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

By default, WordPress does not insert a hyperlink (or permalink) into the excerpt. Without modification, excerpts display […].

That delivery of content is often customized, however, to often include a call-to-action (i.e. Read more) and link to the article in question.

How Can I Add a Read More Link to My WordPress Excerpts?

Again, you’ll need to access your functions.php file to make this modification. Once you’ve done that, you can use the script below to create an excerpt that has a link.

/**
 * Filter the "read more" excerpt string link to the post.
 *
 * @param string $more "Read more" excerpt string.
 * @return string (Maybe) modified "read more" excerpt string.
 */
function wpdocs_excerpt_more( $more ) {
    if ( ! is_single() ) {
        $more = sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
            get_permalink( get_the_ID() ),
            __( 'Read More', 'textdomain' )
        );
    }
 
    return $more;
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

I’ll add more to this post soon. 😉

Here are a few resources for additional context and information on this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *