How To Customize Excerpts in WordPress to Remove the […] Programmatically

WebTechRiser.com > WordPress > How To Customize Excerpts in WordPress to Remove the […] Programmatically

WordPress has some smart features that also require customization. WordPress’s “Excerpt” feature is one such feature. This Excerpt can be best controlled by using the More tag. However, in widgets and some templates where Excerpt is auto-generated, the normal output of the More tag is not taken into account. This is why WordPress has provided us with relief. The limitations of “Excerpt” can be overcome by using custom functions in the functions.php file in your child theme.

What are Excerpts in WordPress?

Excerpts are small summaries or gist of the full post or page content that will give your post audience an overview of what your post is all about. By default, a WordPress theme displays automatically generated excerpts that will show the first few characters of your content including a “read more” link. These excerpts are generated via the WordPress function:

Excerpt in WordPress

Look Before You Leap

To customize the Excerpt in this tutorial, you need to store the custom function in the theme’s functions.php file. Extreme care has to be taken during this type of direct modification. Do it at your own risk. Please download a backup copy of the theme’s functions.php file before the modification, so that we can restore the site to its previous position by restoring the backup copy if the site behaves strangely due to an error in what we are now doing. Changing any of the files in the original theme conflicts with the standard practice of WordPress. This type of work should be done in the functions.php file of the child theme. These customizations will be lost if you install any updates to the original theme.

Remove […] completely

To completely remove […] from Excerpt, open the functions.php file in your child theme and paste the following code:

function webtechriser_trim_excerpt( $text )
{
	return rtrim( $text, '[…]' );
}
add_filter( 'get_the_excerpt', 'webtechriser_trim_excerpt' );

Replace […] with …

The following lines of code will replace “[…]” with ““:

function webtechriser_trim_excerpt( $text )
{
	return str_replace( ' […]', '…', $text );
}
add_filter( 'get_the_excerpt', 'webtechriser_trim_excerpt' );

Replace […] with “Read more…”

This snippet will provide you with a “Read more…” instead of “[…]” from the Excerpt.

function webtechriser_trim_excerpt( $text )
{
	return str_replace( ' […]', '' . ' Read more…', $text );
}
add_filter( 'get_the_excerpt', 'webtechriser_trim_excerpt' );

Customize Excerpt’s […] of Custom Post Type (CPT)

When you’re working with “Custom Post Type” (CPT), you can still customize Excerpt’s “[…]” string to your liking, although the WordPress function works for all Excerpts globally.

function webtechriser_change_excerpt( $more ) {
	if ( post_type_exists( 'news' ) ) {
		return '';
	}

	return '…';
}
add_filter( 'excerpt_more', 'webtechriser_change_excerpt' );

Here in this small snippet in your functions.php, I have managed to replace the “[…]” string of the excerpt of ‘news’ CPT with a “blank” space. However, this function won’t touch excerpts of all other posts and pages and peacefully return the “” string.

Also read:  10 Important WordPress Plugins and Tools for SEO to Rank Your WordPress Website Even Better

Wrapping up

Although this small function of customizing Excerpt’s […] string is very small, it is a necessary and effective function of WordPress. I hope you find this tutorial useful for your theme.

Category WordPress

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.