How to Limit Content in WordPress

By BANAGO » WordPress » February 3rd, 2010

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. StudioPress’s 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 "<p>";
echo $content;
echo "&nbsp;<a href="%22;"></a>
the_permalink();
echo "'>"."Read More &rarr;";
echo "</p>";
}

else >if ((strlen($content)>$max_char) &amp;&amp; ($espacio = strpos($content, " ", $max_char ))) {
$content = substr($content, 0, $espacio);
$content = $content;
echo "<p>";
echo $content;
echo "...";
echo "&nbsp;<a href="%22;"></a>
the_permalink();
echo "'>".$more_link_text."</a>";
echo "</p>";
}

else {
echo "<p>";
echo $content;
echo "&nbsp;<a href="%22;"></a>
the_permalink();
echo "'>"."Read More &rarr;";
echo "</p>";
}
}

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

<?php 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:

<?php excerpt('20');?>
// or
<?php content('20');?>

The end

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.

2 people shared their opinion »

  1. graphicbeacon

    February 14, 2010
    at 9:42 pm

    There is a bug with Studiopress’ approach. Either you or they did not escape some of the double quotes in the echo statements, resulting in some echo statement treated by php as a normal string(check the code). I made some corrections and displayed it as follows, adding the [title] and [rel] attributes so that it will help with validation:

    ', ']]>', $content);
    $content = strip_tags($content);
    if (strlen($_GET['p']) > 0) {
    echo "";
    echo $content;
    ?>
    <a href="" rel="bookmark" title="Permanent Link to ">Read More &rarr;
    $max_char) &amp;&amp; ($espacio = strpos($content, " ", $max_char ))) {
    $content = substr($content, 0, $espacio);
    $content = $content;
    echo "";
    echo $content;
    echo "...";
    ?>
    <a href="" rel="bookmark" title="Permanent Link to ">
    <?php
    }
    
    else {
    echo "";
    echo $content;
    ?>
    <a href="" rel="bookmark" title="Permanent Link to ">Read More &rarr;
    
  2. graphicbeacon

    February 14, 2010
    at 9:45 pm

    It seems your comment form has cut off some of some of my code. Delete this if im wrong.

Leave a comment

Copyright © 2007 - 2009 Baki Goxhaj Powered by WordPress and WPlancer