Home CMS Tutorial WordPress WordPress sets independent Description and Keywords

WordPress sets independent Description and Keywords

Feb 21, 2023 am 11:14 AM
php wordpress

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use &lt;?php comments_template(); ?&gt; tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

See all articles