PHP: Easy Way To Limit Words In a String and WordPress

Writen By Tyler Ingram on Oct 17, 2008

I was looking over my index page and wanted to modify WordPress’ pre-built the_excerpt() function which limits the amount of words displayed for a particular post on my front page without cutting words in half. The draw back from using the built-in the_excerpt() function is that you can not modify how it works, there is not option to give the function arguments such as how many words to display.

So how would I go about creating a function to display the amount of words I want in the way I want it? Well I had to take a look at how I display the content of the front page. This isn’t typical of most blogs but I run custom MySQL query and then loop through each post. Sure this sounds like the WordPress’ Loop but doing it this way allows me to customize the information in the way I want it.

So What Did I Do?

Once I’ve created the MySQL query and get the results I then loop through the results that are found. This is pretty straight-forward and not too difficult to do.

Within the loop I extract the information I need using the setup_postdata($post) function. This allows me to use the built-in functions as well as allows me access to the data in variables for my customization needs.

Now I need an easy way of limited the output of the $post->post_content data I now have access to.

function showBrief($str, $length) {
  $str = strip_tags($str);
  $str = explode(" ", $str);
  return implode(" " , array_slice($str, 0, $length));
}

Seems pretty easy right? I think so. I strip the tags (html tags) from the content so that it won’t show hyperlinks or images and then I explode the information based on the spaces between words so it goes into an array. I then implode (piece together) the array but ensure the array I am piecing back together is only a certain number of words.

Now you just need to echo showBrief($post->post_content, 75) and there you go! It limits the output to 75 words or whatever you specify with ease.

This makes it so that the output doesn’t cut a word off at the character level, so it will only output full words which, I think, looks better than words that have been chopped off.

The other option which is to create the excerpt yourself via the Advanced Options when writing a new Post. Though creating a customized excerpt this way can have its advantages especially for SEO and keyword implementation within a particular post.

Perhaps one day WordPress will allow you to specify arguments for the function the_excerpt(). As you can see from the above code, it shouldn’t be that hard to change right?

Posted in: PHP| WordPress | 1,031 views

 5 Comments

  • You should look at the filters available to WP plugins. I believe you can customize how the excerpt is generated by creating the right filters.

  • I forgot about filters, thanks Ianiv. I’ll have to take a peek at those too. Though I need to also work on moving my custom functions to the functions file in my theme too.

  • How can I limit my string inside a build-in function .I want to limit string only few words and then have a “more links” to link all the author profile.

    Please help me….

  • @Joel you would need to recreate the function and tell WordPress to overwrite the function to use yours. Are you using a particular function now that you want to modify?

  • Actually I am newly in wordpress.I have this in my index and i want to view post only 50 words.Additional,my post have photos that should view also.This is my code:

    <div class=”post” id=”post-”>

    <!– by –>
    <a href=”" rel=”bookmark” title=”Permanent Link to “>

    .stico_default{background:none !important;}

    <a href=”" rel=”bookmark” title=”Permanent Link to “>READ MORE … »

    »

    Not Found
    Sorry, but you are looking for something that isn’t here.