All in One SEO 插件提取中文描述过短解决方法
All in One SEO(也叫AIOSEO)是WordPress中最流行的SEO插件之一,功能非常强大,使初学者和专家都能轻松地在他们的网站上优化SEO。但是All in One SEO有一个小问题就是对中文文章内容页截取的描述过短。
All in One SEO的文章描述长度跟WordPress的摘要长度一样,默认是55个字符,要怎么才能把摘要长度设置长一些呢?网上也有很多方法教程,但是我看了一下基本上是针对All in One SEO 4.0.0之前的版本的修改方案.
以下介绍一下针对4.0.0以上的版本修复方案:
1. 编辑文件:/wp-content/plugins/all-in-one-seo-pack/app/Common/Traits/Helpers/WpContext.php
2. 找到以下代码块
/**
* Returns the description based on the post content.
*
* @since 4.0.0
*
* @param WP_Post|int $post The post (optional).
* @return string The description.
*/
public function getDescriptionFromContent( $post = null ) {
$post = ( $post && is_object( $post ) ) ? $post : $post = $this->getPost( $post );
static $content = [];
if ( isset( $content[ $post->ID ] ) ) {
return $content[ $post->ID ];
}
if ( empty( $post->post_content ) || ! empty( $post->post_password ) ) {
$content[ $post->ID ] = '';
return $content[ $post->ID ];
}
$postContent = $this->getPostContent( $post );
$postContent = wp_trim_words( $postContent, 55, '' );
$postContent = str_replace( ']]>', ']]>', $postContent );
$postContent = preg_replace( '#(<figure.*\/figure>|<img.*\/>)#', '', $postContent );
$content[ $post->ID ] = trim( wp_strip_all_tags( strip_shortcodes( $postContent ) ) );
return $content[ $post->ID ];
}
把wp_trim_words( $postContent, 55, ” )中的55改成你要的长度,比如改成wp_trim_words( $postContent, 120, ” )
3. 以下是修改前和修改后的效果
修改前;
修改后:
转载请注明出处:https://www.luoxudong.com/710.html
本文首发于 东哥小栈(EastStack) · 书写不止是记录,更是思考的延伸。


