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.
问题:
回答1:
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, 300);
}
?>
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.
回答2:
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, '...');?>
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>'
回答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) . " […]";
}
回答4:
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, '...' );
回答5:
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);
回答6:
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);
?>
php substr() function refrence
php strip_tags() function refrence
回答7:
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 );
回答8:
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;
}
}
回答9:
<?php
echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) )
?>