Menu

How to remove auto-paragraph tags in WordPress post excerpts and limit its character length

Today I’m going to show you how to make two quick adjustments to your WordPress site’s post excerpts, both of which you can implement in moments. Moments, I say!

Removing the autop from excerpts automatically

Typically, when you want to display excerpts from your articles, you would add <?php the_excerpt(); ?> to your theme templates. This automatically wraps the excerpt in a paragraph tag. But what if that’s not what you want? Or, what if you need that auto-generated p tag to use a specified class or ID?

Place this in your templates instead:


<?php echo get_the_excerpt(); ?>

You can now wrap your excerpt in any element you wish.

Limiting the length of your post excerpts

On my archive pages, I include post excerpts but when building out my theme, I decided I wanted to keep things a little tighter by limiting the characters displayed in each excerpt to a specified number.

To do likewise, open up your theme’s functions.php file and add the following:


function custom_excerpt_length( $length ) {
        return 45;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

In this example, I’ve limited the excerpt to 45 characters.

Have fun!

Archived under:
Share:
This site uses cookies to collect basic info to help improve your experience. Read my Privacy Policy Accept