首页 > CMS > Wordpress > 正文

WordPress的文章自动添加关键词及关键词的SEO优化

2024-09-07 00:53:09
字体:大 中 小
来源:转载
供稿:网友

网站的关键字及网页描述关系网站对搜索引擎的友好程度,如果自己手动加显然太折腾了,那如何让wordpress博客自动为每篇文章自动关键字及网页描述。每篇文章的内容不同,我们该如何让wordpress自动添加文章描述和关键词呢?下面就让我们来看看如何给wordpress自动添加文章描述和关键词。

在你主题的functions.php文件添加以下代码,各个代码的功能解析如下:

  1. add_action ( 'wp_head', 'wp_keywords' ); // 添加关键字 
  2.  
  3. add_action ( 'wp_head', 'wp_description' ); // 添加页面描述 
  4.  
  5. function wp_keywords() { 
  6.  
  7.  global $s, $post; 
  8.  
  9.  $keywords = ''; 
  10.  
  11.  if (is_single ()) { //如果是文章页,关键词则是:标签+分类ID 
  12.  
  13.  if (get_the_tags ( $post->ID )) { 
  14.  
  15.   foreach ( get_the_tags ( $post->ID ) as $tag ) 
  16.  
  17.   $keywords .= $tag->name . ', '; 
  18.  
  19.  } 
  20.  
  21.  foreach ( get_the_category ( $post->ID ) as $category ) 
  22.  
  23.   $keywords .= $category->cat_name . ', '; 
  24.  
  25.  $keywords = substr_replace ( $keywords, '', - 2 ); 
  26.  
  27.  } elseif (is_home ()) { 
  28.  
  29.  $keywords = '我是主页关键词'; //主页关键词设置 
  30.  
  31.  } elseif (is_tag ()) { //标签页关键词设置 
  32.  
  33.  $keywords = single_tag_title ( '', false ); 
  34.  
  35.  } elseif (is_category ()) {//分类页关键词设置 
  36.  
  37.  $keywords = single_cat_title ( '', false ); 
  38.  
  39.  } elseif (is_search ()) {//搜索页关键词设置 
  40.  
  41.  $keywords = esc_html ( $s, 1 ); 
  42.  
  43.  } else {//默认页关键词设置 
  44.  
  45.  $keywords = trim ( wp_title ( '', false ) ); 
  46.  
  47.  } 
  48.  
  49.  if ($keywords) { //输出关键词 
  50.  
  51.  echo "<meta name="/"keywords/"" content="/"$keywords/"">/n"; 
  52.  
  53.  } 
  54.  
  55. } 
  56.  
  57.   
  58.  
  59. function wp_description() { 
  60.  
  61.  global $s, $post; 
  62.  
  63.  $description = ''; 
  64.  
  65.  $blog_name = get_bloginfo ( 'name' ); 
  66.  
  67.  if (is_singular ()) { //文章页如果存在描述字段,则显示描述,否则截取文章内容 
  68.  
  69.  if (! emptyempty ( $post->post_excerpt )) { 
  70.  
  71.   $text = $post->post_excerpt; 
  72.  
  73.  } else { 
  74.  
  75.   $text = $post->post_content; 
  76.  
  77.  } 
  78.  
  79.  $description = trim ( str_replace ( array ( 
  80.  
  81.   "/r/n", 
  82.  
  83.   "/r", 
  84.  
  85.   "/n", 
  86.  
  87.   " ", 
  88.  
  89.   " " 
  90.  
  91.  ), " ", str_replace ( "/"", "'", strip_tags ( $text ) ) ) ); 
  92.  
  93.  if (! ($description)) 
  94.  
  95.   $description = $blog_name . "-" . trim ( wp_title ( '', false ) ); 
  96.  
  97.  } elseif (is_home ()) {//首页显示描述设置 
  98.  
  99.  $description = $blog_name . "-" . get_bloginfo ( 'description' ) .'首页要显示的描述'; // 首頁要自己加 
  100.  
  101.  } elseif (is_tag ()) {//标签页显示描述设置 
  102.  
  103.  $description = $blog_name . "有关 '" . single_tag_title ( '', false ) . "' 的文章"; 
  104.  
  105.  } elseif (is_category ()) {//分类页显示描述设置 
  106.  
  107.  $description = $blog_name . "有关 '" . single_cat_title ( '', false ) . "' 的文章"; 
  108.  
  109.  } elseif (is_archive ()) {//文档页显示描述设置 
  110.  
  111.  $description = $blog_name . "在: '" . trim ( wp_title ( '', false ) ) . "' 的文章"; 
  112.  
  113.  } elseif (is_search ()) {//搜索页显示描述设置 
  114.  
  115.  $description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' 的搜索結果"; 
  116.  
  117.  } else {//默认其他页显示描述设置 
  118.  
  119.  $description = $blog_name . "有关 '" . trim ( wp_title ( '', false ) ) . "' 的文章"; 
  120.  
  121.  } 
  122. //Vevb.com 
  123.  //输出描述 
  124.  
  125.  $description = mb_substr ( $description, 0, 220, 'utf-8' ) . '..'; 
  126.  
  127.  echo "<meta name="/"description/"" content="/"$description/"">/n"; 
  128.  
  129. } 

突出关键字在搜寻结果:

  1. function wps_highlight_results($text){ 
  2.  
  3. if(is_search()){ 
  4.  
  5. $sr = get_query_var('s'); 
  6.  
  7. $keys = explode(" ",$sr); 
  8.  
  9. $text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong>'.$sr.'</strong>', $text); 
  10. //Vevb.com 
  11. } 
  12.  
  13. return $text; 
  14.  
  15. } 
  16.  
  17. add_filter('the_excerpt', 'wps_highlight_results'); 
  18.  
  19. add_filter('the_title', 'wps_highlight_results'); 

使用此代码段突出显示搜索词在你的博客搜索结果the_excerpt和the_title。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表