wordpress怎么单独设置关键词(description)和描述(keywords)

2025年01月30日 建站教程

wordpress如何单独设置关键词(description)和描述(keywords),下面web建站小编给大家详细介绍一下具体实现代码!

head添加以下代码:

<?php
$description = '';
$keywords = '';
 
if (is_home() || is_page()) {
   $description = "描述";
   $keywords = "关键词";
}
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'));
 
   $description = $description1 ? $description1 : $description2;
    
   $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="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />

本文链接:http://so.lmcjl.com/news/22331/

展开阅读全文
相关内容