How to change excerpt length in WordPress

in Code Write a comment

In this tutorial, you’ll learn how to change the default excerpt length in WordPress.

By default, the excerpt length is set to 55 words. It works well with default themes but may look odd in your custom theme. Luckily, it’s easy to change.

Open the functions.php file and add the following function.

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function custom_excerpt_length( $length ) {
  return 20; // the number of words to display
}

To show post excerpt in template use the code below. It will print post excerpt up to 20 words.


Hi, I'm Renat 👋

 


<?php the_excerpt(); ?>

If you find this post useful, please let me know in the comments below. 
Cheers,
Renat Galyamov

Want to share this with your friends?
👉renatello.com/wordpress-excerpt-length

PS: make sure you check other WordPress tutorials, e.g. create a new sidebar in WordPressload Google Fonts via functions.php in WordPress and download my plugin that improves your WordPress SEO & Performance.

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment

Comment