php 如何在wordpress中对the_content()和the_excerpt()设置字符限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3147898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:51:10  来源:igfitidea点击:

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

phpwordpress

提问by Thomas

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.

如何在 wordpress 中对 the_content() 和 the_excerpt() 设置字符限制?我只找到了字数限制的解决方案 - 我希望能够设置输出的确切数量的字符。

回答by richsage

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

您可以使用 Wordpress 过滤器回调函数。在您的主题目录中,创建一个名为的文件functions.php并将以下内容添加到:

<?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.

plugin_myContentFilter()每次您通过以下方式请求帖子/页面的内容时都会调用该函数the_content()- 它为您提供内容作为输入,并将使用您从该函数返回的任何内容用于后续输出或其他过滤器函数。

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

您也可以对the_exercpt()-add_filter()然后一个用作回调的函数执行相同的操作。

See the Wordpress filter reference docsfor more details.

有关更多详细信息,请参阅Wordpress 过滤器参考文档

回答by Rvervuurt

Or even easier and without the need to create a filter: use PHP's mb_strimwidthto truncate a string to a certain width (length). Just make sure you use one of the get_syntaxes. For example with the content:

或者更简单,无需创建过滤器:使用 PHPmb_strimwidth将字符串截断为特定宽度(长度)。只要确保您使用其中一种get_语法即可。例如内容:

<?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().

这会将字符串剪切为 400 个字符并用.... 只需通过指向带有get_permalink().

<a href="<?php the_permalink() ?>">Read more </a>

Of course you could also build the read morein the first line. Than just replace '...'with '<a href="' . get_permalink() . '">[Read more]</a>'

当然,您也可以read more在第一行中构建。不仅仅是替换'...''<a href="' . get_permalink() . '">[Read more]</a>'

回答by fregante

This also balances HTML tags so that they won't be left open and doesn't break words.

这也平衡了 HTML 标签,这样它们就不会被打开并且不会破坏单词。

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) . " […]";
} 

回答by Purvik Dhorajiya

wp_trim_wordsThis function trims text to a certain number of words and returns the trimmed text.

wp_trim_words此函数将文本修剪为一定数量的单词并返回修剪后的文本。

Example:-

例子:-

echo wp_trim_words( get_the_content(), 40, '...' );

回答by Bhavin Suvaliya

For Using the_content()functions (for displaying the main content of the page)

用于使用the_content()功能(用于显示页面的主要内容)

$content = get_the_content();

echo substr($content, 0, 100);

For Using the_excerpt()functions (for displaying the excerpt-short content of the page)

用于使用the_excerpt()功能(用于显示页面的摘录短内容)

$excerpt= get_the_excerpt();

echo substr($excerpt, 0, 100);

回答by w411 3

Replace <?php the_content();?>by the code below

替换<?php the_content();?>为下面的代码

<?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 substr() 函数引用

php strip_tags() function refrence

php strip_tags() 函数参考

回答by Aamer Shahzad

wp_trim_words()This function trims text to a certain number of wordsand returns the trimmed text.

wp_trim_words()此函数将文本修剪为一定数量的单词并返回修剪后的文本。

$excerpt = wp_trim_words( get_the_content(), 40, '<a href="'.get_the_permalink().'">More Link</a>');

Get truncated string with specified widthusing mb_strimwidth()php function.

使用php 函数获取指定宽度的截断字符串mb_strimwidth()

$excerpt = mb_strimwidth( strip_tags(get_the_content()), 0, 100, '...' );

Using add_filter()method of WordPress on the_contentfilter hook.

add_filter()WordPress 在the_content过滤器钩子上的使用方法。

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.

使用自定义 php 函数来限制内容字符。

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 );

回答by Ali Tariq

<?php 
echo apply_filters( 'woocommerce_short_description', substr($post->post_excerpt, 0, 500) ) 
?>

回答by Mohammed Sufian

just to help, if any one want to limit post length at home page.. then can use below code to do that..

只是为了帮助,如果有人想将帖子长度限制在home page.. 然后可以使用下面的代码来做到这一点..

the below code is simply a modification of @bfred.itSir

下面的代码只是对@bfred.it先生的修改

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;
  }

}