Implementing Custom Excerpts in WordPress Themes

Posted on 18th June 2023

Themes are what make WordPress so special. Without them, WordPress would just be another content management system. But with themes, you can transform WordPress into anything you can imagine.

One of the most important aspects of a WordPress theme is the excerpt. The excerpt is what appears on the blog homepage, in archive pages, and in search results. It’s what gives your readers a taste of what the post is about before they click through to read the whole thing.

The default WordPress excerpt is pretty basic. It just takes the first few sentences of your post and calls it a day. But with a little bit of code, you can customize the excerpt to better suit your needs.

In this article, we’re going to show you how to implement custom excerpts in WordPress themes. We’ll cover the basics of what an excerpt is and how it works. Then we’ll dive into some code and show you how to customize the excerpt for your own needs.

Let’s get started!

What is an Excerpt?

An excerpt is a summary of a post. It’s typically displayed on the blog homepage, in archive pages, and in search results. The goal of an excerpt is to give the reader a taste of what the post is about so they can decide if they want to read it.

The default WordPress excerpt is pretty basic. It just takes the first few sentences of your post and calls it a day.

This can be problematic for a few reasons. First, the excerpt might not accurately represent the content of the post. Second, the excerpt might be too long or too short.

With a custom excerpt, you have complete control over what appears in the excerpt. You can hand-craft an excerpt that accurately represents the post and is the perfect length.

How to Implement Custom Excerpts in WordPress Themes

Now that we’ve covered the basics, let’s dive into some code and show you how to customize the excerpt for your own needs.

The first thing you need to do is add a new function to your theme’s functions.php file. This function will be responsible for creating the custom excerpt.

function wpdocs_custom_excerpt_length( $length ) {
return 30;
}
add_filter( ‘excerpt_length’, ‘wpdocs_custom_excerpt_length’, 999 );

This function sets the length of the excerpt to 30 words. You can change this number to whatever you like. Just make sure it’s a number greater than 0.

Next, we need to add a filter to our function. This filter will tell WordPress to use our custom function instead of the default excerpt function.

add_filter( ‘get_the_excerpt’, ‘wpdocs_custom_excerpt_length’, 999 );

Now, when WordPress generates an excerpt, it will use our custom function.

Conclusion

In this article, we’ve shown you how to implement custom excerpts in WordPress themes. We’ve covered the basics of what an excerpt is and how it works. Then we’ve dove into some code and showed you how to customize the excerpt for your own needs.

If you want to learn more about customizing WordPress, be sure to check out our other articles on the subject.

The default excerpt length in WordPress is 55 words, but you can change this by going to Settings > Reading in your WordPress dashboard and changing the “Excerpt length” field.

You can also change the way the excerpt is generated by using the “Excerpt more” field. This field allows you to specify what should be used to replace the […] that is automatically added to the end of the excerpt.

If you want to generate your excerpts manually, you can do so by using the the_excerpt() template tag. This tag will return the excerpt of the post, but will not display it.

To display the excerpt, you will need to use the echo statement:

echo the_excerpt();

If you want to generate a custom excerpt, you can do so by using the get_the_excerpt() function. This function allows you to specify the length of the excerpt, as well as what should be used to replace the […] that is automatically added to the end of the excerpt.

The get_the_excerpt() function has the following syntax:

get_the_excerpt( $post_id, $length, $more );

The $post_id parameter is the ID of the post whose excerpt you want to retrieve. The $length parameter is the number of words that you want to include in the excerpt. The $more parameter is the string that you want to use to replace the […] that is automatically added to the end of the excerpt.

Here is an example of how to use the get_the_excerpt() function:

$excerpt = get_the_excerpt( $post->ID, 50, ‘Read More’ );

echo $excerpt;

This code will output the excerpt of the post with an ID of $post->ID . The excerpt will be 50 words long, and the […] will be replaced with the string “Read More”.

If you want to display the excerpt without the […] , you can use the strip_tags() function:

$excerpt = strip_tags( get_the_excerpt( $post->ID ) );

echo $excerpt;

If you want to display the excerpt without the […] and without any HTML tags, you can use the wp_strip_all_tags() function:

$excerpt = wp_strip_all_tags( get_the_excerpt( $post->ID ) );

echo $excerpt;

When adding a custom excerpt to a WordPress theme, there are a few things to keep in mind. First, the excerpt length should be controlled by the WordPress admin in the Settings > Reading section. Second, the excerpt should be added to the theme using the the_excerpt() function, and not hard-coded into the theme.

Third, the excerpt should be wrapped in a div with the class “entry-excerpt”. This will ensure that the excerpt looks consistent with the rest of the content on the page. Finally, it is important to remember to add a link to the full post after the excerpt. This can be done using the the_permalink() function.

Adding a custom excerpt to a WordPress theme is a great way to control the content that is displayed on the front page of the site. By following the guidelines above, you can ensure that your custom excerpts are consistent with the rest of the content on the site.