Pages Menu
TwitterRssFacebook
Categories Menu

Posted by in Wordpress Tutorials | 0 Comments

Best Approach: How to Limit Words of WordPress Content / Excerpt

Advertisement

In this article, we’re sharing a best and easy to use approach for limiting the number of words of WordPress content / excerpt. Your situation might be different, but this solution will work in most cases.

 

You might find these useful

function limit_words($string, $word_limit)
{
  $words = explode(" ",$string);
  return implode(" ",array_splice($words,0,$word_limit));
}

$myContent = get_the_content();
echo limit_words($myContent, 9);

You can use this code for displaying posts excerpt as well. But for excerpts, you’ll have to add one line like below:

function limit_words($string, $word_limit)
{
  $words = explode(" ",$string);
  return implode(" ",array_splice($words,0,$word_limit));
}

$myContent = get_the_content();
$myExcerpt = strip_tags($myContent);
echo limit_words($myExcerpt, 9);

Here, we’re using strip_tags() function to remove html elements around the text.

Post a Reply

Your email address will not be published. Required fields are marked *


× three = 15

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>