WordPress 6.4 摘要失效问题解决:wp_trim_words()这个函数失效了
// 很多主体是用下面的代码输出摘要的,如果是get_the_excerpt()就没有问题 &n…
// 很多主体是用下面的代码输出摘要的,如果是get_the_excerpt()就没有问题
// echo wp_trim_words( get_the_content(), 40, '...' ); // 用这个代码输出摘要的,就会全部出内容了
// wp_trim_words()这个函数失效了
// 测试
// echo wp_trim_words('abcdefg',5,' […]') ; // 正确的是输出5个字符,现在是全部输出。
// 替换方法
/**
* 字符截断函数
* @since onlyfront 1.0
* @$str 要截取的字符串
* @$start 开始位置
* @$long 长度
* @$trimmarker 后缀
*/
/*function short_string($str ,$start , $long ,$trimmarker ){
$output = mb_substr($str,$start,$long,'utf-8');
$strlen = mb_strlen($str,'utf-8');
if($strlen > $long){
return $output.$trimmarker;
}else{
return $output;
}
}
// wp_strip_all_tags() 去掉内容的所有标签,只留下纯文本
echo short_string(wp_strip_all_tags($post->post_content,true),0,55,'...');// 输出55个文字摘要
// 合理的方法如下,当然还得看你自己的主题代码是怎样的,自己改一下
/**
* 自动获取摘要
* @since onlyfront 1.0
*/
/*function wper_auto_excerpt($excerpt) {
global $post;
if ( has_excerpt() && ! is_attachment() ) {
$excerpt = wp_strip_all_tags($post->post_excerpt,true);
}else{
$seo_desc = get_post_meta($post->ID,'seo_desc',true);
if($seo_desc){
$excerpt = $seo_desc;
}else{
$excerpt = short_string(wp_strip_all_tags($post->post_content,true),0,55,'...');
// 更新摘要 二选一,下次获取摘要就不会再次重复截取拉低速度了
// 1、更新摘要到自定义字段
//update_post_meta($post->ID,'seo_desc',$excerpt);
// 2、更新摘要到post_excerpt
wp_update_post( array(
'ID' => $post->ID,
'post_excerpt' => $excerpt
) );
}
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'wper_auto_excerpt' );
//*************/
类别:WordPress经验、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!