saber 酱的抱枕

Fly me to the moon

12/31
2014
学习

wordpress自动设置keywords和description

在主题的header.php中把keywords和description部分的代码改成下面这样:

<?php
if ( is_single ())	//文章页面
{
if ( $post->post_excerpt)
{
$description = $post->post_excerpt;
} else {
$description = mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,220);
}
$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords.$tag->name.","; }
}else if ( is_home ())	//主页
{
$description = "saber酱的抱枕是一个以saber的主题的博客网站,分享一些ACG资源和福利,也会记录生活和学习中的一些事情。";
$keywords = "saber,saber酱,福利,WordPress, dedecms";
}else{		//其他页面,如分类目录、单页面等
	$description = "saber酱的抱枕是一个以saber的主题的博客网站,分享一些ACG资源和福利,也会记录生活和学习中的一些事情。";
	$keywords = "saber,saber酱,福利,WordPress, dedecms";
}
?>
<meta name = "keywords" content="<?php echo $keywords; ?>" />
<meta name= "description" content="<?php echo $description; ?> "/ >

这样,在首页就会显示自己设置的关键词和简介了。在文章页则会把文章的tag作为关键词,摘取文章开头作为页面简介。

在分类目录里和单页面里我设置的同主页,你也可以设置成别的,或考虑再改进。

参考:

is_home()    是否为主页
is_single()   是否为内容页(Post)
is_page()    是否为页面页(Page)
is_category()   是否为分类页(Category/Archive)
is_author()  是否是作者页面

更多参考

wordpress自动设置keywords和description