默认下载安装的WordPress主题提供的keywords和description这两个meta标签一般都做得很差,或者根本就不提供,这样不利于SEO。本文将指导你如何给主页、分类、页面以及文章页添加单独的Description 和 Keywords。

很多人都在用All in One SEO插件解决这个问题,但是又有很多人说这个插件不好。大不了咱就自己用代码实现吧。

下面提供露兜博客设置keywords和description的代码,将以下代码添加至WordPress主题header.php的<head></head>之间,最后用UTF-8编码另存为,否则中文会乱码:

[cc lang=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”php\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”]ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”description\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, true); $description2 = str_replace(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”,mb_strimwidth(strip_tags($post->post_content), 0, 200, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”…\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’utf-8\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’));

// 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述 $description = $description1 ? $description1 : $description2;

// 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词 $keywords = get_post_meta($post->ID, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”keywords\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, true); if($keywords == \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) { $tags = wp_get_post_tags($post->ID); foreach ($tags as $tag ) { $keywords = $keywords . $tag->name . \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”; } $keywords = rtrim($keywords, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’); } } elseif (is_category()) { $description = category_description(); $keywords = single_cat_title(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, false); } elseif (is_tag()){ $description = tag_description(); $keywords = single_tag_title(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, false); } $description = trim(strip_tags($description)); $keywords = trim(strip_tags($keywords)); ?><meta name=\\\\\\\\\\\\\\\\\\\\\\\”description\\\\\\\\\\\\\\\\\\\\\\\” content=\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” /><meta name=\\\\\\\\\\\\\\\\\\\\\\\”keywords\\\\\\\\\\\\\\\\\\\\\\\” content=\\\\\\\\\\\\\\\\\\\\\\\”\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\” /> [/cc] 以后在写博客文章时只需添加两个自定义栏目(在文章编辑页面下面,没有就点击右上角的显示选项)即可,第一个自定义栏目名称为keywords,栏目值写上这篇文章的关键字。接着再添加第二个自定义栏目,自定义栏目名称为description,后面的栏目值写上这篇日志的描述。自定义栏目用过一次后,以后再写日志只需在下拉框中选择即可。这样每篇文章都有你自定义的keywords和description了,你完全可以抛弃All in One SEO插件了。

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部