用了一段时间发现wordpress的主题不会自动生成网站的描述(description)和关键字(keywords),网上找了一下,还真就有解决方案,看过文章后,那是相当激动了!
解决方法:
/wp-content/themes/当前使用的模版目录/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?>" />