Thoughts On Web Design

Custom WordPress Excerpt Length

Custom WordPress Excerpt Length

In this post we are going to set the default WordPress post excerpt length and allow for different excerpt lengths in different templates.

Set Default Excerpt Length

We are going to set the default excerpt length.

1. Adjust as needed and add the following code to your theme's functions.php file.

function custom_excerpt_length() {
global $customExcerptLength;

if ($customExcerptLength) {
return $customExcerptLength;
} else {

// Replace 40 with your default excerpt length.
return 40; //default value
}
}
add_filter('excerpt_length', 'custom_excerpt_length');

Different Excerpt Lengths For Different Templates

We are adding a variable to set the length of the excerpt to the template code that shows the excerpt.

1. Adjust as needed and add the following code to your theme's template file where you would like to show the excerpt. The code below will show a 20 word excerpt.

// Change 20 to whatever length you would like for your excerpt.
<?php $customExcerptLength=20; echo get_the_excerpt(); ?>

THANKS FOR READING
Thank you for reading Custom WordPress Excerpt Length. I hope you enjoyed it. You can give me feedback, say hi or yell at me on twitter.com/beuinteractive.


Mar 25, 2024 Filed under WordPress

Reading

The Maltese Falcon

Life and Death in The Andes

Ender's Game

War and Peace

Dune

Brewing Up A Business

Latest Tweets


Follow Us On Twitter