Code

How to Limit Content in WordPress

This is an old article describing old-fashioned ways of limiting content. I’m publishing a new article on this topic very soon so make sure you subscribe below.

Have you seen those cool magazine style WordPress themes around this days? They are just cool. You might have noticed that they sometimes show a few words of the post, this is for sure not the default WordPress excerpt showing that because the default excerpt length is set to 55 words and manually excerpting every post would be a great time consumer. So, what are this guys putting together to get that cool effect on their sites by controlling the length of the content? Well, as a WordPress freelancer, I have been needing that feature myself and today I’m going to share with you how to have that feature on your site.

There are three different approaches to limit your excerpt or content in WordPress that you must know:

1. WordPress’s Approach

WordPress developers have provided us lately with a tool to limit the excerpt. I do not use it myself as I’m more conformable with another approach as I find this a little limited. To change excerpt length using the default WordPress excerpt limit approach you have to use the excerpt_length filter and add the following code to functions.php file in your theme:

function new_excerpt_length($length) {
      return 20;
 }
add_filter('excerpt_length', 'new_excerpt_length');

Every time you will use the_excerpt(); on your code it will output an 20-word text string for you. Right now you’ve got a question, don’t you? I know, I had it too. The question is: What if I want to use the 20-word excerpt in some places on my theme, but still be able to use the 55-word one? Or just have two or three different-length exerts to use on my theme? Well, if that is the case, the default approach is not for you, so continue reading.

2. The StudioPress Approach

I don’t really know who came up with this approach originally, but I know that the guys over at StudioPress use it on their themes. This function is called the_content_limit(). This is how the code that will go into the functions.php file looks like – lengthy huh?

function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') { 
$content = get_the_content($more_link_text, $stripteaser, $more_file); 
$content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); 
$content = strip_tags($content); 
if (strlen($_GET['p']) > 0) { 
echo ""; echo $content; echo " "."Read More →"; echo ""; 
} 
else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) { 
$content = substr($content, 0, $espacio); $content = $content; echo ""; 
echo $content; 
echo "..."; 
echo "".$more_link_text."
"; echo ""; 
} else { 
echo ""; echo $content; echo " 
"."Read More →
"; echo ""; 
} }

Now we have to call that function because, off course, we are not going to use the default the_excerpt(); function. Here:

the_content_limit(20, "[Read more]");

Pretty simple to use and offers you the possibility to auto-generate a Read More … of you choice.

C.bavota’s Apprach

C.bacota – I don’t know his real name – has produced a couple of great code snippets to limit the content for both the_content() and the_excerpt(). As you may already know, the_excerpt() gets 55 words from the_content() but get rids of all the HTML tags on it, be it links, images or whatever. So, this guy produced a script that allows you to choose to use the text-only version or the html-rich version to limitedly display. I find this functions really cool and easy to use and I have been using them ever since they were produced. This is how the block of code that goes into functions.php looks like:

// Limit the content to certain number of words.
 function excerpt($num) {
 $limit = $num+1;
 $excerpt = explode(' ', get_the_excerpt(), $limit);
 array_pop($excerpt);
 $excerpt = implode(" ",$excerpt)."...";
 echo $excerpt;
 }
 // Limit the content to certain number of words.
 function content($num) {
 $limit = $num+1;
 $content = explode(' ', get_the_content(), $limit);
 array_pop($content);
 $content = implode(" ",$content)."...";
 echo $content;
}

The above code is quite cool, but has two limitation: The first block of code, will not allow you to have a custom excerpt longer than 55 words, the same as the_excerpt() does, and the second one will bump image into your shorten content if you are using images in the original one. However, Mr. C.Basoda eventually thought about his and produced later a revised version of the code that will allow you to use more than 55 words and not get images on the wrong place there. Here is the revised code:

function excerpt($limit) {
 $excerpt = explode(' ', get_the_excerpt(), $limit);
 if (count($excerpt)>=$limit) {
 array_pop($excerpt);
 $excerpt = implode(" ",$excerpt).'...';
 } else {
 $excerpt = implode(" ",$excerpt);
 }
 $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
 return $excerpt;
 }
function content($limit) {
 $content = explode(' ', get_the_content(), $limit);
 if (count($content)>=$limit) {
 array_pop($content);
 $content = implode(" ",$content).'...';
 } else {
 $content = implode(" ",$content);
 }
 $content = preg_replace('/[.+]/','', $content);
 $content = apply_filters('the_content', $content);
 $content = str_replace(']]>', ']]>', $content);
 return $content;
 }

And finally here is how you use this fantastic piece of code:

excerpt('20');

// or
content('20');

Final Thoughts

We are finally done.  I would also like to make clear that the approaches I presented so far are not necessary all the existing or possible ways to achieve content limitation, however this is what I know and what I thought was of value to share with you. Thanks for reading.

Baki Goxhaj
I’m a web consultant specializing in WordPress. I love elegant solutions, quality code and a good laughter.
You may also like
An Introduction to Python Programming #2
Minimalist WordPress Theme Development