如何在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;
}

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

wprec推荐插件模板变量文档以及样式推荐wprec推荐插件模板变量文档以及样式推荐

wpac是一款wordpress自动配图插件,可以丰富文章内容,对提升排名有很大帮助。p style=”font-size:18px;”>你可能还喜欢下面这些文章<p>{excerpt}<

wprec - wordpress相关文章插件,最好的相似推荐插件wprec – wordpress相关文章插件,最好的相似推荐插件

一个理想的相关文章推荐插件应该是什么样子的?wprec就是一个能够提升用户体验,提升搜索引擎排名的相关文章推荐插件!插件的后台在 WP工具箱-文章推荐,进入即可看到设置。

wordpress网站怎么设置不可被复制wordpress网站怎么设置不可被复制

原创内容经常被别人轻易复制转载?站长工具箱中自带内容保护插件,可禁止右键和复制功能,使用十分方便。登录WordPress后台,依次点击【外观】-【编辑】,找到footer.php并编辑,在<

WordPress 阅读设置WordPress 阅读设置

在本章中,我们将研究WordPress中的Reading Settings。 您可以设置要在主页上显示的帖子数。步骤(2) – 读取设置页面如下所示显示。Blog pages show at most(博客页面最多显示) – 每页或网站要

WordPress 写作设置WordPress 写作设置

写入设置控制写入体验,并提供自定义WordPress网站的选项。 要使用此功能,您需要设置具有POP3访问权限的秘密电子邮件帐户,并且将发布在此地址收到的任何邮件。

wordpress 静态化怎么设置wordpress 静态化怎么设置

现在你只需创建一个404错误页面,并且写入下列4行代码即可简单优雅的实现无插件完美支持windows iis主机的永久固定链接的伪静态化地址格式。这样即可固定REQUEST_URI和PATH_INFO参量并且包括进去index.php,剩下的

wordpress如何设置固定链接wordpress如何设置固定链接

进入设置 -> 固定链接 进行设置。如果你正在使用某些主机面板,那么面板里面应该有比较方便的设置,以宝塔面板为例,点击网站右侧的设置,选择wordpress伪静态规则,如下图

WordPress 固定链接设置WordPress 固定链接设置

在本章中,我们将了解WordPress中的Permalink settings。Custom Structure(自定义结构) – 它通过在给定文本框中输入所需的名称来设置您选择的网址结构。

好看 (0) 很好看 (0) 非常好看 (0)