以下代码实现的是以标签为关键词;以摘要为描述,如果没有填写摘要,那就自动截取文章前 200 字为描述

代码实现 WordPress 自动关键词与描述:

以下代码放到你的主题下 funtions.php 的最后一个 ?> 前:

  1. function get_cats_name() {
  2. $allcats=get_categories();
  3. foreach ($allcats as $category)
  4. {
  5. $keywords[] = $category->cat_name;
  6. }
  7. return $keywords;
  8. }
  9. function utf8Substr($str$from$len) {
  10. return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
  11. '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',
  12. $1',$str);

  13. }
  14. function meta_SEO() {
  15. global $post;
  16. $output = '';
  17. if (is_single()){
  18. $keywords = '';
  19. $description = '';
  20. if ($post->post_excerpt) {
  21. $description = $post->post_excerpt;
  22. $description = str_replace("rn","",$description);
  23. $description = str_replace("n","",$description);
  24. $description = str_replace(""","'",$description);
  25. $description .= '...';
  26. else {
  27. $description = utf8Substr(strip_tags($post->post_content),0,200);
  28. $description = str_replace("rn","",$description);
  29. $description = str_replace("n","",$description);
  30. $description = str_replace(""","'",$description);
  31. $description .= '...';
  32. }
  33. $tags = wp_get_post_tags($post->ID);
  34. foreach ($tags as $tag ) {
  35. $keywordarray[] = $tag->name;
  36. }
  37. $keywords = implode(',',array_unique((array)$keywordarray));
  38. else {
  39. $keywords = 'WordPress,WordPress 主题,WordPress 教程,WordPress 主题下载,WordPress 网站主题,WordPress 企业主题,WordPress 主题定制'; 
  40. $description = '主题猫,致力于为广大网友提供最新最全的 WordPress 主题';
  41. }
  42. $output .= '<meta name="keywords" content="' . $keywords . '" />' . "n";
  43. $output .= '<meta name="description" content="' . $description . '" />' . "n";
  44. echo "$outputn";
  45. }

第 43 行与第 44 行的内容需要根据你的网站进行修改。