用the_excerpt处理中文文章字数限制的方法

用the_excerpt处理中文文章字数限制的方法

wordpress处理the_excerpt的代码,你可以在wp-includes目录下的formatting.php中找到,通过代码,你可发现the_excerpt是用空格来计算长度的,这对英文是可以的,但是对中文,基本上不可行。

formatting.php中的代码如下:

01 function wp_trim_excerpt($text '') {
02     $raw_excerpt $text;
03     if '' == $text ) {
04         $text = get_the_content('');
05  
06         $text = strip_shortcodes( $text );
07  
08         $text = apply_filters('the_content'$text);
09         $text str_replace(']]>'']]>'$text);
10         $excerpt_length = apply_filters('excerpt_length', 55);
11         $excerpt_more = apply_filters('excerpt_more'' ' '[...]');
12         $text = wp_trim_words( $text$excerpt_length$excerpt_more );
13     }
14     return apply_filters('wp_trim_excerpt'$text$raw_excerpt);
15 }

中文文章的长度问题就是出在$text = implode(‘ ‘, $words);

解决方式

在您自己主题下面的functions.php文件中添加如下代码就可以解决了

1 function excerpt_read_more_link($output) {
2  global $post;
3  $output = mb_substr($output,0, 200);
4  return $output '<span><a href="'. get_permalink($post->ID).'">阅读全文...</a></span>';
5  }
6 add_filter('the_excerpt''excerpt_read_more_link');

快看看您的文章吧,效果还不错吧。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用WordPress的默认函数`WP_Query()`来获取指定时间范围内的文章列表。以下是一个示例代码: ``` <?php $today = getdate(); // 获取当前日期 $args_today = array( 'post_type' => 'post', 'date_query' => array( array( 'year' => $today['year'], 'month' => $today['mon'], 'day' => $today['mday'], ), ), 'orderby' => 'date', 'order' => 'DESC', ); $args_yesterday = array( 'post_type' => 'post', 'date_query' => array( array( 'year' => $today['year'], 'month' => $today['mon'], 'day' => $today['mday'] - 1, ), ), 'orderby' => 'date', 'order' => 'DESC', ); $args_7days = array( 'post_type' => 'post', 'date_query' => array( array( 'after' => '-7 days', // 查询7天前到现在的文章 'before' => $today['year'] . '-' . $today['mon'] . '-' . $today['mday'], // 查询今天之前的文章 'inclusive' => true, ), ), 'orderby' => 'date', 'order' => 'DESC', ); // 查询今天的文章 $the_query = new WP_Query( $args_today ); if ( $the_query->have_posts() ) : echo '<h3>今天的文章</h3>'; while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>'; echo '<div>' . get_the_excerpt() . '</div>'; endwhile; endif; wp_reset_postdata(); // 查询昨天的文章 $the_query = new WP_Query( $args_yesterday ); if ( $the_query->have_posts() ) : echo '<h3>昨天的文章</h3>'; while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>'; echo '<div>' . get_the_excerpt() . '</div>'; endwhile; endif; wp_reset_postdata(); // 查询7天内的文章 $the_query = new WP_Query( $args_7days ); if ( $the_query->have_posts() ) : echo '<h3>7天内的文章</h3>'; while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<h2><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>'; echo '<div>' . get_the_excerpt() . '</div>'; endwhile; endif; wp_reset_postdata(); ?> ``` 这段代码会分别查询今天的文章、昨天的文章和7天内的文章,并按照时间倒序输出所有文章的标题和摘要。如果需要输出更多信息,可以在循环中添加对应的WordPress函数。`wp_reset_postdata()`可以用来重置查询,以便在后续的代码中查询其他内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值