WordPress安装好,默认是没有Description和Keywords,网上也有很方法介绍添加Description和Keywords,但好多都是无法使用的,互相抄袭,根本不起作用,在添加过程中,也遇到些问题,比如添加了Description和Keywords不生效,或者加Description和Keywords之后,用站长工具查询,是乱码,下面记录下添加方法(这里是使用WordPress 6.3版本测试可用):
1、到你的主题模板下,找到header.php,一般此文件位于wp-content/themes/你的主题名称/header.php
把此文件下载下来,编辑打开,最好用专用的编辑工具UE或Notepad++或者Dreamweaver打开。
在header.php文件,最前面添加如下内容:
<?php
//判断是否为首页
if (is_home()) {
$description = '这里填写描述';
$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、用站长工具在次查询时,注意勾选更新网页缓存