北极寒流 » 网络资源 » wordpress自动生成描述(description)和关键字(keywords)的方法 - 2016.03.09

wordpress自动生成描述(description)和关键字(keywords)的方法

网上有些wordpress主题不会自动生成描述(description)和关键字(keywords),其实只要把下面代码插入主题header.php的<head></head>中间适当位置即可。

<?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.', ';}}?>
<?php if (is_home()){
$description = "【改为wordpress首页网站描述】";
$keywords = "【改为wordpress首页网站关键字】";
} elseif (is_single()){
$description = mb_strimwidth(strip_tags($post->post_content),0,400);//400才能自动截取200字左右作为文章描述,可按需修改
$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?>" />

资料来源:http://ssk.91txh.com/54