Home > CMS Tutorial > WordPress > body text

WordPress sets independent Description and Keywords

青灯夜游
Release: 2023-02-21 19:43:33
forward
2257 people have browsed it

The meta tags keywords and description provided by the WordPress theme you download are generally poorly made, or not provided at all, which is not conducive to SEO. This article will guide you how to add separate Description and Keywords to the homepage, categories, pages, and article pages.

Many people are using the All in One SEO plug-in to solve this problem, but many people say that this plug-in is not good. At worst, let's implement it ourselves with code.

The editor's code for setting keywords and description is provided below. Add the following code to the WordPress theme header.php between

, and finally save it as UTF-8 encoding, otherwise Chinese characters will be garbled:

<?php
$description = &#39;&#39;;
$keywords = &#39;&#39;;

if (is_home() || is_page()) {
   // 将以下引号中的内容改成你的主页description
   $description = "露兜博客描述";

   // 将以下引号中的内容改成你的主页keywords
   $keywords = "WordPress, 博客, 编程,php,ludou";
}
elseif (is_single()) {
   $description1 = get_post_meta($post->ID, "description", true);
   $description2 = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", &#39;utf-8&#39;));

   // 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述
   $description = $description1 ? $description1 : $description2;
   
   // 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词
   $keywords = get_post_meta($post->ID, "keywords", true);
   if($keywords == &#39;&#39;) {
      $tags = wp_get_post_tags($post->ID);    
      foreach ($tags as $tag ) {        
         $keywords = $keywords . $tag->name . ", ";    
      }
      $keywords = rtrim($keywords, &#39;, &#39;);
   }
}
elseif (is_category()) {
   // 分类的description可以到后台 - 文章 -分类目录,修改分类的描述
   $description = category_description();
   $keywords = single_cat_title(&#39;&#39;, false);
}
elseif (is_tag()){
   // 标签的description可以到后台 - 文章 - 标签,修改标签的描述
   $description = tag_description();
   $keywords = single_tag_title(&#39;&#39;, false);
}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
?>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
Copy after login

When writing blog posts in the future, you only need to add two custom columns (under the article editing page, if not, click Show Options# in the upper right corner) ##), the first custom column name is keywords, and the column value contains the keywords of this article. Then add a second custom column, the name of the custom column is description, and the subsequent column value contains the description of this log. After using a custom column once, you only need to select it in the drop-down box to write a log in the future. In this way, each article has your customized keywords and description, and you can completely abandon the All in One SEO plug-in.

The description and keywords of the home page and page are written directly using PHP code. You can change the description of the Pandan Blog above to your own.

The description of the category page and tag page (tag) directly takes the description of the category or tag. This can be changed in the WordPress management background as needed, and keywords takes the name of the category or tag. If you want to manually define description and keywords for each category, you can change lines 27-28 of the code to:

if (is_category(&#39;音乐&#39;)) {
    $description = &#39;这里填音乐分类的描述&#39;;
    $keywords = &#39;这里填音乐分类的关键字,半角逗号隔开&#39;;
}
elseif (is_category(&#39;电影&#39;)) {
    $description = &#39;这里填电影分类的描述&#39;;
    $keywords = &#39;这里填电影分类的关键字,半角逗号隔开&#39;;
}
elseif (is_category(&#39;戏剧&#39;)) {
    $description = &#39;这里填戏剧分类的描述&#39;;
    $keywords = &#39;这里填戏剧分类的关键字,半角逗号隔开&#39;;
}

// 多个分类,复制9-12行代码,粘贴到13后面行,类似5-12行,改一下分类名称即可
// 使用UTF-8编码另存为,替换原来的文件,否则可能会乱码
Copy after login
-- End--

Recommended learning :《

WordPress Tutorial

The above is the detailed content of WordPress sets independent Description and Keywords. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:ludou.org
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template