Limit the Character Length of the Content or Excerpt in WordPress


Categories
Tags
You might have some lengthy excerpts or content in some of your posts and pages, which are taking up too much space when listing them on your website. Learn how to limit the length of these when querying WordPress, so you can display a more ‘trimmed down’ list of posts/pages.

And this to your functions file…

function custom_content_length($content) {
	return mb_strimwidth($content, 0, 300, '...');	//cuts the string at 300 characters and closes it with ...
}

As a function, this can easily be used site-wide and is adjustable in one central place (in your functions file). Simply adjust the character limit to suit your needs.

Next, call your excerpt/content like so…

$content = get_the_excerpt();
echo custom_content_length($content);

This code will pass your excerpt/content onto the function, which cuts the string length and adds 3 dots to the end (but only if it exceeds 300 characters), before returning the newly formatted output for you to echo on screen.

This is a great technique to use on archive and category-based pages.

Comments

Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the author(s) and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views. Nothing negative please.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).

Thank you – we hope you find the website useful.