一段精简代码:
<?php if (is_single()){
if($post->post_excerpt){
$description=strip_tags($post->post_excerpt);
}
else{
$description=substr(strip_tags($post->post_content),0,110);
}
$keywords='';
$tags=wp_get_post_tags($post->ID);
foreach($tags as $tag){
$keywords=$keywords.$tag->name.', ';}
}
?>
<?if (is_home()){
$description = "网站描述";
$keywords = "网站关键字";
} elseif (is_single()){
$description = mb_strimwidth(strip_tags($post->post_content),0,180); //自动截取180字作为文章描述,可按需修改
$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ", ";
}
} elseif(is_category()){
$description = category_description();
}
?>
<meta name="description" content="<?=$description?>" />
<meta name="keywords" content="<?=$keywords?>" />
代码还会自动调用标签作为文章关键字,话说wordpress标签的权重也很高啊。。
from:http://byncc.com/05-wordpress-keywords-meta.html