How to Implement Custom Post Excerpts in WordPress

Posted on 19th June 2023

The excerpt is a summary of the content of a post, usually presented in a list format. It is typically used to provide a brief summary of the content of a post before the reader decides whether to read the full post or not. In WordPress, the excerpt is generated automatically by the content management system (CMS) when a post is created. However, there may be times when you want to create a custom excerpt for a post, or even for all posts on your WordPress site.

In this article, we will show you how to easily create custom post excerpts in WordPress, without having to edit the code of your theme.

What is a Post Excerpt?

An excerpt is a summary of the content of a post, usually presented in a list format. It is typically used to provide a brief summary of the content of a post before the reader decides whether to read the full post or not.

In WordPress, the excerpt is generated automatically by the content management system (CMS) when a post is created. The excerpt is usually generated by taking the first few sentences of the post content. However, there may be times when you want to create a custom excerpt for a post, or even for all posts on your WordPress site.

When and Why You Should Use a Custom Post Excerpt?

There are a few situations where you might want to use a custom post excerpt in WordPress.

If you are using an automated system to post content from another source (like an RSS feed), then the excerpt can be used to create a summary of the post content.

Some WordPress themes display the post excerpt on the homepage, category pages, and tag pages. In these cases, you might want to use a custom excerpt to control what appears on these pages.

If you are using WordPress as a content management system (CMS) for a website, then you might want to use custom excerpts to create summaries of pages that are displayed in the navigation menu.

In general, using a custom post excerpt gives you more control over how your content is displayed on your WordPress site.

How to Add a Custom Excerpt in WordPress

Adding a custom excerpt in WordPress is very easy. First, you need to edit the post or page where you want to add the custom excerpt.

On the post edit screen, you will see the excerpt field below the post editor.

You can simply add your custom excerpt in this field and then click on the “Update” button to save your changes.

How to Use a Custom Post Excerpt in WordPress

Once you have added a custom excerpt to a post or page in WordPress, it will be used in place of the automatically generated excerpt.

In most cases, the custom excerpt will be used on the homepage, category pages, and tag pages.

Some WordPress themes also give you the option to display the custom excerpt in other places such as the post meta information, social share buttons, and related posts sections.

How to Create a Custom Post Excerpt for All Posts in WordPress

If you want to create a custom excerpt for all posts on your WordPress site, then you can do that by editing your theme files.

However, we don’t recommend that you edit your theme files directly. If you make a mistake, then it can break your website.

Instead, we recommend that you use a child theme. A child theme is a theme that inherits the functionality of another theme, called the parent theme.

If you make changes to a child theme, then it will not be overwritten when you update the parent theme. This allows you to make changes to your theme without having to worry about losing them when you update the parent theme.

Once you have created a child theme, you can add this code to your child theme’s functions.php file.

function my_custom_excerpt( $post_id ) {

$post = get_post( $post_id );

$excerpt = $post->post_content;

// remove shortcodes
$excerpt = strip_shortcodes( $excerpt );

// remove images
$excerpt = preg_replace( ‘/(]+) class=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) style=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) width=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) height=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) alt=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) title=”.*?”/i’, ‘$1’, $excerpt );

// remove comments
$excerpt = preg_replace( ‘//’, ”, $excerpt );

// remove

and
tags
$excerpt = preg_replace( ‘/(]+)>/i’, ”, $excerpt );

// remove empty lines
$excerpt = preg_replace( ‘/^s+|s+$/m’, ”, $excerpt );

// limit to 300 words
$excerpt = wp_trim_words( $excerpt, 300, ‘…’ );

return $excerpt;

}

add_filter( ‘the_excerpt’, ‘my_custom_excerpt’ );

This code will create a custom post excerpt for all posts on your WordPress site.

How to Limit the Length of the Custom Post Excerpt

By default, WordPress will create a custom excerpt for all posts on your WordPress site. However, you can change this behavior by adding a few lines of code to your child theme’s functions.php file.

function my_custom_excerpt( $post_id ) {

$post = get_post( $post_id );

$excerpt = $post->post_content;

// remove shortcodes
$excerpt = strip_shortcodes( $excerpt );

// remove images
$excerpt = preg_replace( ‘/(]+) class=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) style=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) width=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) height=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) alt=”.*?”/i’, ‘$1’, $excerpt );
$excerpt = preg_replace( ‘/(]+) title=”.*?”/i’, ‘$1’, $excerpt );

// remove comments
$excerpt = preg_replace( ‘//’, ”, $excerpt );

// remove

and
tags
$excerpt = preg_replace( ‘/(]+)>/i’, ”, $excerpt );

// remove empty lines
$excerpt = preg_replace( ‘/^s+|s+$/m’, ”, $excerpt );

// limit to 300 words
$excerpt = wp_trim_words( $excerpt, 300, ‘…’ );

return $excerpt;

}

add_filter( ‘the_excerpt’, ‘my_custom_excerpt’ );

This code will create a custom excerpt for all posts on your WordPress site. However, it will only create an excerpt for posts that have the “excerpt” field in the post editor.

How to Use a Custom Post Excerpt in WordPress

Once you have added a custom excerpt to a post or page in WordPress, it will be used in place of the automatically generated excerpt.

In most cases, the custom excerpt will be used on the homepage, category pages, and tag pages.

Some WordPress themes also give you the option to display the custom excerpt in other places such as the post meta information, social share buttons, and related posts sections.

How to Create a Custom Post Excerpt for All Posts in WordPress

If you want to create a custom excerpt for all posts on your WordPress site, then you can