How To Limit Words Count For Post Excerpt or Content In WordPress

Published on July 4th, 2022

By


It is always a good idea to show an excerpt of a post instead of full post content on a blog’s landing page. This not only reduces download time, as a full post is not downloaded, but it also makes the page look clutter-free. One can get an excerpt either automatically through the_excerpt() or get_the_excerpt() WordPress functions but by entering an excerpt manually for each post. In the auto-generated excerpt, all shortcodes and tags are removed and by default and there is a 55 words limit. Generating a post excerpt can also be done using various plugins.

Sometimes displaying 55 words is not enough and you want to display more words in the excerpt or if you want to display very little in the excerpt so in which case customization is required. And we can limit the number of words for not just the contents or excerpt but for any custom field as well.

Limiting the words or characters during the display can be done by either creating a function in the function.php file or right in the code itself using the WordPress function wp_trim_words() or mb_strimwidth() etc.

There are two ways to display content using the_content() and get_the_content() function. One important difference between them is that the_content() echos the contents without removing tags or shortcodes from the content whereas get_the_content() removes tags and any shortcodes from the content before displaying. Another difference is we need to use ‘echo’ with get_the_content() whereas the_content() directly echos the content without using ‘echo’.

To limit the number of words to 40 for a custom field (custom_field_name) and have a ‘… more’ link after that, below is the code to achieve the same. We are using wp_trim_words() function and finally echoing the value in the end.

//assign content of a custom field
$content = get_post_meta($post->ID, custom_field_name, true);

$trimmed_content = wp_trim_words($content, 40, '...<a href="' . get_permalink() . '">more</a>');

echo $trimmed_content;

Now to limit the number of characters, instead of words, is to use a PHP function mb_strimwidth(). Some people may get a fatal error “Call to undefined function mb_strimwidth() in …” which is due to mb_* PHP extension not installed on their server.

Please check for ‘php-mbstring‘ package on the server and install or activate as the case may be for the function to work.


$content = get_the_content();

echo mb_strimwidth($content, 0, 215, '...'); // limit to 215 characters

In a post loop, below is how we may limit the title of a post to say 15 characters and put a ‘…’ after that.


if (strlen($post->post_title) > 15)
echo substr($post->post_title, 0, 18) . ' ...';
else
echo $post->post_title;

Here are few more free resource tips that you might find useful. How to edit the WordPress excerpt box label to any label you want, what to do when the ‘no shipping options available‘ issue appears in your WooCommerce store, or if you are developing a job board website using Simple Job board plugin, learn how you can customize it.

How To Limit Words Count For Post Excerpt or Content In WordPress was last modified: July 4th, 2022 by Creative Admin

Comments are closed.