解决WordPress网站无描述和关键词问题

68次阅读
没有评论

共计 2049 个字符,预计需要花费 6 分钟才能阅读完成。

网络上有不少的优秀 WordPress 主题,但是因为某些原因,开发者未能充分考虑到 SEO 问题,导致不少的 WordPress 主题源代码中只有标题,却没有描述(description)和关键词(keyword)两个标签内容,为此,博主去网上查阅了一番,将解决方法记录如下:

<?php 
if (is_home() ) {?><title><?php bloginfo('name'); ?> | <?php bloginfo('description'); ?>
</title><?php } ?>
<?php if (is_search() ) {?><title> 搜索结果 | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (is_single() ) {?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (is_page() ) {?><title><?php echo trim(wp_title('',0)); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (is_category() ) {?><title><?php single_cat_title(); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (is_month() ) {?><title><?php the_time('F'); ?> | <?php bloginfo('name'); ?></title><?php } ?>
<?php if (function_exists('is_tag')) {if ( is_tag() ) {?><title><?php  single_tag_title("", true); ?> | <?php bloginfo('name'); ?></title><?php } ?> <?php } ?>
<?php if (is_author() ) {?><title><?php wp_title('');?> 发表的所有文章 | <?php bloginfo('name'); ?></title><?php }?>
<?php
if (!function_exists('utf8Substr')) {function utf8Substr($str, $from, $len)
 {return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
          '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',

          '$1',$str);
 }
}
if (is_single() ){if ($post->post_excerpt) {$description  = $post->post_excerpt;} else {if(preg_match('/<p>(.*)</p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){$post_content = $result['1'];
   } else {$post_content_r = explode("n",trim(strip_tags($post->post_content)));

    $post_content = $post_content_r['0'];

   }
         $description = utf8Substr($post_content,0,220); 
  }
    $keywords = "";    

    $tags = wp_get_post_tags($post->ID);

    foreach ($tags as $tag) {$keywords = $keywords . $tag->name . ",";}
}
?>
<?php echo "n"; ?>
<?php if (is_single() ) { ?>
<meta name="description" content="<?php echo trim($description); ?>" />
<meta name="keywords" content="<?php echo rtrim($keywords,','); ?>" />
<?php } ?>
<?php if (is_home() ) { ?>
<meta name="description" content="首页描述" />
<meta name="keywords" content="首页关键字" />
<?php } ?>

然后在主题目录中新建一个 title.php 文件,将以上代码添加到里面,然后打开 header.php 文件,调用如下代码:

<?php include('title.php'); ?>

如此,网站的描述和关键字都可以正常展示了,当然,上面的代码加了 <title> 标签,如果只想修改 keyword、description 的话就可以将其删除。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-06-03发表,共计2049字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)