'How to set character limit on the_content() and the_excerpt() in wordpress
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.
Solution 1:[1]
Or even easier and without the need to create a filter: use PHP's mb_strimwidth to truncate a string to a certain width (length). Just make sure you use one of the get_ syntaxes.
For example with the content:
<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>
Update 2022
mb_strimwidth breaks the HTML if the comment tag is used. use official wordpress wp_trim_words functions
<?php $content = get_the_content(); echo wp_trim_words( get_the_content(), 400, '...' );?>
This will cut the string at 400 characters and close it with ....
Just add a "read more"-link to the end by pointing to the permalink with get_permalink().
<a href="<?php the_permalink() ?>">Read more </a>
Of course you could also build the read more in the first line. Than just replace '...' with '<a href="' . get_permalink() . '">[Read more]</a>'
Solution 2:[2]
wp_trim_words This function trims text to a certain number of words and returns the trimmed text.
Example:-
echo wp_trim_words( get_the_content(), 40, '...' );
Solution 3:[3]
This also balances HTML tags so that they won't be left open and doesn't break words.
add_filter("the_content", "break_text");
function break_text($text){
$length = 500;
if(strlen($text)<$length+10) return $text;//don't cut if too short
$break_pos = strpos($text, ' ', $length);//find next space after desired length
$visible = substr($text, 0, $break_pos);
return balanceTags($visible) . " […]";
}
Solution 4:[4]
For Using the_content() functions (for displaying the main content of the page)
$content = get_the_content();
echo substr($content, 0, 100);
For Using the_excerpt() functions (for displaying the excerpt-short content of the page)
$excerpt= get_the_excerpt();
echo substr($excerpt, 0, 100);
Solution 5:[5]
Replace <?php the_content();?> by the code below
<?php
$char_limit = 100; //character limit
$content = $post->post_content; //contents saved in a variable
echo substr(strip_tags($content), 0, $char_limit);
?>
Solution 6:[6]
wp_trim_words() This function trims text to a certain number of words and returns the trimmed text.
$excerpt = wp_trim_words( get_the_content(), 40, '<a href="'.get_the_permalink().'">More Link</a>');
Get truncated string with specified width using mb_strimwidth() php function.
$excerpt = mb_strimwidth( strip_tags(get_the_content()), 0, 100, '...' );
Using add_filter() method of WordPress on the_content filter hook.
add_filter( "the_content", "limit_content_chr" );
function limit_content_chr( $content ){
if ( 'post' == get_post_type() ) {
return mb_strimwidth( strip_tags($content), 0, 100, '...' );
} else {
return $content;
}
}
Using custom php function to limit content characters.
function limit_content_chr( $content, $limit=100 ) {
return mb_strimwidth( strip_tags($content), 0, $limit, '...' );
}
// using above function in template tags
echo limit_content_chr( get_the_content(), 50 );
Solution 7:[7]
just to help, if any one want to limit post length at home page .. then can use below code to do that..
the below code is simply a modification of @bfred.it Sir
add_filter("the_content", "break_text");
function limit_text($text){
if(is_front_page())
{
$length = 250;
if(strlen($text)<$length+10) return $text; //don't cut if too short
$break_pos = strpos($text, ' ', $length); //find next space after desired length
$visible = substr($text, 0, $break_pos);
return balanceTags($visible) . "... <a href='".get_permalink()."'>read more</a>";
}else{
return $text;
}
}
Solution 8:[8]
<?php
echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) )
?>
Solution 9:[9]
I know this post is a bit old, but thought I would add the functions I use that take into account any filters and any <![CDATA[some stuff]]> content you want to safely exclude.
Simply add to your functions.php file and use anywhere you would like, such as:
content(53);
or
excerpt(27);
Enjoy!
//limit excerpt
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
//limit content
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Uxair |
| Solution 2 | |
| Solution 3 | fregante |
| Solution 4 | ulentini |
| Solution 5 | w411 3 |
| Solution 6 | Aamer Shahzad |
| Solution 7 | gitdemon |
| Solution 8 | Pathik Vejani |
| Solution 9 |
