正在加载小宠物
梦回起点
做你害怕做的事,你会发现:不过如此
本站基于WordPress—主题by 设计窝 \n
冀ICP备15003737号
梦回起点
Copyright © 2015-2024 All rights reserved.

wordpress通过代码实现自动添加文章关键词和描述

将下面的代码添加到你使用主题的footer.php中

//自动关键词与描述 Devework.com
function get_cats_name() {
$allcats=get_categories();
foreach ($allcats as $category) 
{
$keywords[] = $category->cat_name;
}
return $keywords;
}
// utf8 substr
function utf8Substr($str, $from, $len) {
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
'$1',$str);
}
// Meta SEO
function meta_SEO() {
global $post;
$output = '';
if (is_single()){//如果是文章页
$keywords = ''; 
$description = '';
if ($post->post_excerpt) {//如果文章摘要存在就以文章摘要为描述
$description = $post->post_excerpt;
$description = str_replace("\r\n","",$description);
$description = str_replace("\n","",$description);
$description = str_replace("\"","'",$description);
$description .= '...';
} else {//如果文章摘要不存在就截断文章前200字为描述
$description = utf8Substr(strip_tags($post->post_content),0,200);
$description = str_replace("\r\n","",$description);
$description = str_replace("\n","",$description);
$description = str_replace("\"","'",$description);
$description .= '...';
} 
$tags = wp_get_post_tags($post->ID);//取文章标签
foreach ($tags as $tag ) {
$keywordarray[] = $tag->name;
}
//以文章标签为关键字
$keywords = implode(',',array_unique((array)$keywordarray));
} else {//如果不是文章页
$keywords = '这里填入关键字'; //在引号间写入你博客的关键字用,断开
$description = '这里填入描述';//在引号间写入你博客的简单描述,不要过200字
}
//输出关键字
$output .= '' . "\n";
$output .= '' . "\n";
//输出描述
echo "$output\n";
}
add_action('wp_head', 'meta_SEO');//添加meta_SEO函数到头部信息里

修改非文章页的描述和关键词   这样就可以了


参考资料:

https://ztmao.com/jiaocheng/1318.html

http://www.moke8.com/article-12307-1.html

2017-10-16
暂无评论

发表回复