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

更新于 2021年12月26日 wordpress教程

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

解决方案

您可以使用 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);
  }
?>

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

您还可以将add_filter()用于其他函数,例如the_excerpt()以在请求摘录时提供回调函数。

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

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

<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '...');?>

这会将字符串剪切为 400 个字符并用...关闭它。 只需通过使用get_permalink()指向永久链接,在末尾添加“阅读更多”链接。

<a href="https://stackoverflow.com/questions/3147898/how-to-set-character-limit-on-the-content-and-the-excerpt-in-wordpress/<?php the_permalink() ?>">Read more </a>

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

这也平衡了 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) . " […]";
} 

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

例子:-

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

用于使用the_content()函数(用于显示页面的主要内容)

$content = get_the_content();

echo substr($content, 0, 100);

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

$excerpt= get_the_excerpt();

echo substr($excerpt, 0, 100);

用下面的代码替换<?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() 函数引用

php strip_tags() 函数参考

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

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

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

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

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

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

使用自定义 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 );

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

下面的代码只是对@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;
  }

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

我知道这篇文章有点旧,但我想我会添加我使用的函数,这些函数考虑到任何过滤器和任何你想要安全排除的<![CDATA[some stuff]]>内容。

只需添加到您的 functions.php 文件并在您想要的任何地方使用,例如:

content(53);

或者

excerpt(27);

享受!

//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(']]>', ']]&gt;', $content);
  return $content;
}

你可能还喜欢下面这些文章

WordPress admin bar 添加自定义链接菜单WordPress admin bar 添加自定义链接菜单

bar中添加自定义链接菜单,您可以在主题的functions.php文件中添加代码。以下是一个示例代码,演示如何添加自定义链接菜单:在上述代码中,表示在admin。在这个示例中,我们添加了一个顶级菜单项,其ID为,标题为“自定义链接”,链接为

如何处理 WP 主题的 js 中文本的翻译如何处理 WP 主题的 js 中文本的翻译

中访问翻译,例如,您应该使用wp_localize_script函数,正是出于这个原因,它被添加到。/wp-content/plugins/jobhunt-client-translations/jobhunt-client-translat

WordPress文章编辑器页面右侧的分类展示使用的是什么函数?WordPress文章编辑器页面右侧的分类展示使用的是什么函数?

WordPress文章编辑器页面右侧的分类展示使用的是wp_terms_checklist()函数。该函数用于显示一个包含分类目录或标签的复选框菜单,允许用户在文章编辑页面中选择相关的分类或标签。

如何将自定义 HTML 添加到 wp_nav_menu?如何将自定义 HTML 添加到 wp_nav_menu?

我正在尝试创建一个这样的菜单:请注意产品下的下拉菜单如何包含图像和链接。是您的菜单项,如果您愿意,您可以使用它根据当前菜单项查询其他内容。将使用您自定义的类和函数,以便您可以修改输出的代码。

如果文件夹不存在,则创建一个文件夹如果文件夹不存在,则创建一个文件夹

//developer.wordpress.org/reference/functions/wp_mkdir_p/。它返回上传目录路径并创建一个文件夹(如果尚不存在)。

如何在 WordPress 中查找 wp_head() 渲染的 HTML ?如何在 WordPress 中查找 wp_head() 渲染的 HTML ?

//codex.wordpress.org/Function_Reference/wp_head按照https。//codex.wordpress.org/Function_Reference/wp_headwp_head()。

Polylang:如何翻译自定义字符串?Polylang:如何翻译自定义字符串?

//polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/pll_register_string允许插件在“字符串翻译”面板

如何在WordPress文本小工具中运行PHP代码如何在WordPress文本小工具中运行PHP代码

文件:add_filter(‘widget_text’,。php_text($text)。$text;}之后,可以将一个文本小工具添加到侧边栏中,并在其中输入PHP函数代码,看看是不是可以正常运行了。