How to limit the_content word in WordPress

I have search many times to google, how can i limit the_content word so that i can add it to my new project. Finally i have found a solution and thanks to stackoverflow. After crafted it in my project, i have found one bug and here i will share how i solve this. First discuss about code then we will solve problem altogether.

Code – We will use WordPress filter callback function. In your function.php file paste the following code.

<?php 

  add_filter("the_content", "plugin_myContentFilter");
  function plugin_myContentFilter($content)
  {
    // Take the existing content and return a subset of it
    return substr($content, 0, 300);
  }

?>

The plugin_myContentFilter will be called each time when you add  the_content in post/page. From above code, you can change 300 number as your wish for changing word limit.

Problem

If you add above code in function.php file, it will effect on your article is_single page. I have used many method to implement it from function.php file but i have failed.  So i will suggest you to add it in your particular php file. Example below of <?php get_header(); ?>



Leave a Reply