By default, WordPress displays the first 55 words in excerpts. Let’s see how this number can be changed using the excerpt_length filter.

We need to add the following code:

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/**
 * Change the length of excerpt.
 *
 * @param int $length The number of words. Default 55.
 * @return int New excerpt length.
 */
function custom_excerpt_length( $length ) {

    return 20; // number of words. Default is 55.

}

Change 20 to your desired excerpt length.

In Oxygen (or any WordPress site for that matter), there are at least two ways in which code snippets like these can be added.

This will work with the output of Easy Posts element as well as the pre-built Blog components in the Library.

Method 1

Install and activate the Code Snippets plugin.

Add a new Snippet. Give a title of say “Excerpt length” and paste in the code. Select “Only run on site front-end”. Then save the changes and activate.

Method 2

Install and activate my custom functionality plugin.

Add the above code inside wp-content/plugins/my-custom-functionality/plugin.php using a FTP client.

Source: https://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

For adding code blocks wrap the code in three backticks. Markdown should work.
Provide a URL of your site/webpage if something is not working.
  • Hello Sridhar
    I followed your tutorial to reduce the number of words of an excerpt via the code snippets plugin.

    Until this, no particular issues. But I’d like to call the PHP function the_excerpt into a text field (Insert Data > PHP Function return value > Function name > the_excerpt -> [oxygen ct_sign_sha256='blahblahblah' data='phpfunction' function='the_excerpt' ]). But the number of words is not reduced. Instead the whole excerpt is displayed.

    Is it the correct function called?

  • magnifiercross
    >
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
    %d bloggers like this: