无插件自动给WordPress文章添加description头部标签

虽然我们都知道"All in One SEO Pack"插件是可以自动且自定义设置文章的关键字(keyword)、描述(description)的,但是有些时候能精简的就尽量的精简。这里有一个办法可以快速且自动的给每篇文章设置自动描述。

function wp_auto_desc() {
global $post;
if (!is_single()) { return; }
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 300);//数字300可以自己调整
echo "<meta name='description' content='$meta' />";
}
add_action('wp_head', 'wp_auto_desc');

我们可以将脚本添加到当前主题的Functions.php文件中,其中默认是300字符长度,我们可以根据需要调整长度。

版权声明:
作者:牛部落
链接:https://www.tnell.com/151.html
来源:天牛博客
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>