0

Hello I would to truncate my post content to 500 words on my single page post Please how can I do this using PHP?

  • Can you rephrase what you mean by "nothing less than 500 words"? Are you trying to truncate the content length? Or are you trying to show only posts with more than 500 words? Is this when editing a post or for posts already published? It's not clear what you're asking – Tom J Nowell Sep 9 at 9:56
  • 1
    Yes I want to truncate content length – Oluwatemitope Adeala Sep 9 at 10:29
  • Is this truncated on save, truncated on display, or is this a word/character limit when editing a post? – Tom J Nowell Sep 9 at 10:51
  • What's I want to do is reduce the number of post on single post page ( I'm not creating a blog) so I want all post to be short not less than 500 words – Oluwatemitope Adeala Sep 9 at 16:22
  • "not less than 500 words" if taken literally means all posts must be longer than 500 words, which isn't what you want. You might have meant "No more than 500 words", or "500 words or less" – Tom J Nowell Sep 9 at 16:35
0

You could use a WordPress filter callback function. In your theme's directory, create a file called functions.php and add the following in:

<?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, 500);
  }
?>

The plugin_myContentFilter() function will be called each time you request the content of a post/page via the_content() - it provides you with the content as an input, and will use whatever you return from the function for subsequent output or other filter functions.

You can also do the same for the_exercpt() - add_filter() and then a function to be used as a callback.

See the WordPress filter reference docs for more details here http://codex.wordpress.org/Plugin_API/Filter_Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.