WordPress 如何添加keywords和description

一段代码让wordpress博客自动添加关键词(keywords)及网页描述(description)

可以让wordpress博客自动添加关键词(keywords)及网页描述(description)
先打开你的模板文件wp-content/themes/模板目录文件/header.php
在头部添加

  1. <?php
  2. //判断是否为首页
  3. if ( is_home ())
  4. {
  5. $description = “这里填写博客首页的描述,最多220字符”;
  6. $keywords = “这里填写首页关键词,用英文逗号隔开”;
  7. //判断是否为文章页
  8. }
  9. else if ( is_single ())
  10. {
  11. if ( $post->post_excerpt)
  12. {
  13. $description = $post->post_excerpt;
  14. } else {
  15. $description = mb_strimwidth(strip_tags(apply_filters(‘the_content’,$post->post_content)
  16. ),0,220);
  17. }
  18. $keywords = “”;
  19. $tags = wp_get_post_tags($post->ID);
  20. foreach ($tags as $tag ) {
  21. $keywords = $keywords.$tag->name.“,”; }
  22. //判断是否为分类页
  23. } else if ( is_category()) {
  24. $description = category_description();
  25. }
  26. ?>

 

注释可以删除
然后在

<mate>

后面添加

  1. <meta name = “keywords” content=”<?php echo $keywords; ?>” />
  2. <meta name= “description” content=”<?php echo $description; ?> “/ >

保存文件,再刷新一下你的首页看看,是不是就行了呢

 

4人评论了“WordPress 如何添加keywords和description”

发表评论

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