给WordPress添加Description和Keywords
WordPress安装好,默认是没有Description和Keywords,网上也有很方法介绍添加Description和Keywords,但好多都是假牙,互相抄袭,根本不起作用,在添加过程中,也遇到些问题,比如添加了Description和Keywords不生效,或者加Description和Keywords之后,用站长工具查询,是乱码,下面记录下添加方法:
1、到你的主题模板下,找到header.php,一般此文件位于wp-content/themes/你的主题名称/header.php
把此文件下载下来,编辑打开,最好用专用的编辑工具UE或Notepad++打开。
在header.php文件,最前面添加如下内容
<?php
//判断是否为首页
if (is_home()) {
$description = '这里填写你的博客首页的描述,最多220字符';
$keywords = '这里填写你的博客首页的关键词,用英文逗号分开';
//判断是否为文章页
} else 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_category()) {
$description = category_description();
}
?>
2.、查找标签:
</head>
在前面加入
<meta name=”keywords” content=”<?php echo $keywords; ?>” />
<meta name=”description” content=”<?php echo $description; ?>” />
3、把文件上传上去,替换header.php
4、刷新缓存,然后查看是否调整过来
注:在修改的过程中,请注意以下几点
1、数据库字符编码是否为utf-8
2、第2步添加内容时,注意是英文状态下的双引号
3、用站长工具在次查询时,注意勾选更新网页缓存
参考 :https://www.dimmye.com/archives/78
评论