WordPress sets up separate Description and Keywords

Add the following code to the <head></head> of the WordPress theme header.php, and finally save it as UTF-8, otherwise the Chinese will be garbled:

<?php
$description = '';
$keywords = '';

if (is_home() || is_page()) {
   // Change the following quotation marks to your homepage description
   $description = "Pandou Blog Description";

   // Change the following quotation marks to your homepage keywords
   $keywords = "WordPress,blog,programming,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, "…", 'utf-8'));

   // Display the content of the custom field when filling in the custom field description, otherwise use the first 200 words of the article content as the description
   $description = $description1 ? $description1 : $description2;
   
   // Display the content of the custom field when filling in the custom field keywords, otherwise use the article tags as the keyword
   $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()) {
   // The description of the classification can go to the background-article-category directory to modify the description of the classification
   $description = category_description();
   $keywords = single_cat_title('', false);
}
elseif (is_tag()){
   // The description of the tag can go to the background - article - tag to modify the description of the tag
   $description = tag_description();
   $keywords = single_tag_title('', 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; ?>" />

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524382&siteId=291194637