top-arrow

How To Work With WordPress Post Excerpt

WordPress Post Excerpt

The feature of WordPress post excerpt is really helpful and works really cool.
But by default, it doesn’t work as we want. And also it doesn’t make us feel good easy to get the functionality of WordPress post excerpt.

So that’s why I would like to talk about WordPress post excerpt system as well as how to modify it as we want.

I would like to divide it into several sections so that you guys can get it properly.

So these sections are listed below.

How to ?

  1. Modify length of excerpt ( it counts as words ).
  2. Change default ‘[..]’ mark of “read more” to any word the way we want.
  3. Replace text of WordPress post excerpt using the_excerpt hook
  4. Create post excerpt length which counts character rather than words.

What you’ll need to know to understand the concept of WordPress post excerpt.

As PHP is the core language of WordPress, you need to know at least basic syntax of PHP.
You need to understand about WordPress hook system and how it works.

1. How to modify length of WordPress post excerpt.

We will use excerpt_length hook for that. There are two kinds of hook in WordPress ( action and filter). This one is filter.

Such as WordPress Twenty Seventeen Theme shows by default this length ( an example is provided below )

wordpress post excerpt default length

After applying our custom length it will look like the below one.

wordpress post excerpt default length

function webextent_modifying_post_exceprt_length( $def_length ){
    // let’s check we are in admin area or not if not then we will modify it.
   if ( ! is_admin() ) {
       $def_length = 50;
    }
    // otherwise it’ll show the default length
    return $def_length;
}
add_filter( 'excerpt_length', 'webextent_modifying_post_exceprt_length', 999 );

Note: It’ll work only if your Theme use get_the_excerpt() or the_excerpt() function.

2. How to change default ‘[…]’ read more mark to any word we want.

By default after the excerpt, WordPress shows this mark ( [..] ) as read more mark. But we may need to change a custom one such as “read more” or “Find more” etc. So here we will discuss how to modify default read more text of WordPress post excerpt system.

Such as by default Twenty Seventeen Theme shows ‘Continue reading’ if you use WordPress post excerpt.

wordpress post excerpt default length

 

And after applying our custom text it will look like this one.

wordpress post excerpt custom read more text

We will use excerpt_more hook for that. It’s a filter hook.

function webextent_excerpt_more_modify( $def_text ) {
	if ( ! is_admin() ) {
		$text = __('Read More', 'textdomain');
		$link = get_the_permalink();
		$def_text = sprintf("&nbsp;<a href='%s'> %s</a>", $link, $text);
	}
        return $def_text;
}
add_filter('excerpt_more', 'webextent_excerpt_more_modify', 999 );

Note: the_excerpt() or get_the_excerpt() function should be used in your Theme.

3. How to replace text of WordPress post excerpt using the_excerpt filter hook.

For example, we would like to replace some words such as ( dolor, Nulla, tortor ) dummy text to a static word let’s take “REPLACED” for this to test.

function webextent_replacing_text($default_text){
	if ( ! is_admin() ) {
		$replaced_text = __('REPLACED', 'textdomain');
		$default_text = str_replace( array('dolor', 'Nulla', 'tortor' ), $replaced_text, $default_text  );
	}
	return $default_text;
}
add_filter('the_excerpt', 'webextent_replacing_text', 999 );

Here is the output of the code

Replace WordPress default post excerpt text

Note: You can also use get_the_excerpt filter hook to do the exact same thing.

4. How to force WordPress post excerpt to count characters rather than words.

By default, it counts words in a post for WordPress excerpt length. But we can force it to count characters instead of words. We will use wp_trim_excerpt filter hook for that.

So let’s do it.

function webextent_triming_excerpt( $default_text ) {
	if ( ! is_admin() ) {
		// get_the_content() returns post content
		$default_text = get_the_content();
		// let's clear shortcode from get_the_content() function
		$default_text = strip_shortcodes( $default_text );

		// get first 60 characters we can also define where to start
		$default_text = substr( $default_text, 0, 60 );

		// we can add a custom read more sign there.

		$default_text .= ' ...';

	}
	return $default_text;
}

add_filter('wp_trim_excerpt', 'webextent_triming_excerpt', 10, 1 );

Note: the_excerpt() or get_the_excerpt() function should be used in your Theme.

I’m using Twenty seventeen so here is the output.

counts character rather than words wp text excerpt

Here I’ve written an interesting article on how to add body class to your WordPress site ( conditionally or without condition )

Tags:

0 comments

Leave a Reply

Mohammad Rahat
about me

Mohammad Rahat Tanjid

Mohammad Tanjid is a professional WordPress Developer and Designer, he builds WP Products like WP Themes/Plugins and author of webextent.net. He has been writing code for more than 5 years now.