以下程式碼實現的是以標籤為關鍵詞;以摘要為描述,如果沒有填寫摘要,那就自動擷取文章前 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 行的內容需要根據你的網站進行修改。