Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set character limit on the_content() and the_excerpt() in wordpress

Tags:

How do I set a character limit on the_content() and the_excerpt() in wordpress? I have only found solutions for the word limit - I want to be able to set an exact amount characters of outputted.

like image 644
Thomas Avatar asked Jun 30 '10 09:06

Thomas


People also ask

How do I set character limits in WordPress?

Limiting the words or characters during the display can be done by either creating a function in the function. php file or right in the code itself using the WordPress function wp_trim_words() or mb_strimwidth() etc. There are two ways to display content using the_content() and get_the_content() function.

How do I limit the number of words for post excerpt or content in WordPress?

The easiest way to limit excerpt length in WordPress is by adding 'Read More' tag in your post, however you can limit excerpt length in WordPress by controlling number of words or number of characters using Excerpt Length Filter (excerpt_length) Reference.

How do I add a content limit in WordPress?

Using add_filter() method of WordPress on the_content filter hook. Using custom php function to limit content characters. Show activity on this post. just to help, if any one want to limit post length at home page .. then can use below code to do that..


1 Answers

You could use a Wordpress filter callback function. In your theme's directory, locate or 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, 300);   } ?> 

The plugin_myContentFilter() is a function you provide that will be called each time you request the content of a post type like posts/pages 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 use add_filter() for other functions like the_excerpt() to provide a callback function whenever the excerpt is requested.

See the Wordpress filter reference docs for more details.

like image 126
richsage Avatar answered Sep 28 '22 02:09

richsage