用了一段时间发现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?>" />
自动生成描述和关键字?会经常变动的吗?
首页需要单独设置不会变动,如果是文章的话,会自动提取每篇文章开头的N个字(可设置)作为description,然后提取写文章时设置的关键词作为本页的keywords,用了以后感觉还不错,您可以用站长工具检测一下本站的首页和文章就知道效果了~~
哦哦,明白了。学习了,有空捣鼓一下