How to set character limit on the_content() and th

2020-02-02 06:51发布

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.

标签: php wordpress
9条回答
来,给爷笑一个
2楼-- · 2020-02-02 07:09

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-02 07:10
<?php 
echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) ) 
?>
查看更多
干净又极端
4楼-- · 2020-02-02 07:14

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 );
查看更多
Melony?
5楼-- · 2020-02-02 07:15

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);
查看更多
可以哭但决不认输i
6楼-- · 2020-02-02 07:24

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, '...' );
查看更多
Fickle 薄情
7楼-- · 2020-02-02 07:28

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>'

查看更多
登录 后发表回答