以下程式碼實現的是以標籤為關鍵詞;以摘要為描述,如果沒有填寫摘要,那就自動擷取文章前 200 字為描述。
程式碼實現 WordPress 自動關鍵詞與描述:
以下程式碼放到你的主題下 funtions.php 的最後一個 ?> 前:
- function get_cats_name() {
- $allcats=get_categories();
- foreach ($allcats as $category)
- {
- $keywords[] = $category->cat_name;
- }
- return $keywords;
- }
- 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',
- }
- function meta_SEO() {
- global $post;
- $output = '';
- if (is_single()){
- $keywords = '';
- $description = '';
- if ($post->post_excerpt) {
- $description = $post->post_excerpt;
- $description = str_replace("rn","",$description);
- $description = str_replace("n","",$description);
- $description = str_replace(""","'",$description);
- $description .= '...';
- } else {
- $description = utf8Substr(strip_tags($post->post_content),0,200);
- $description = str_replace("rn","",$description);
- $description = str_replace("n","",$description);
- $description = str_replace(""","'",$description);
- $description .= '...';
- }
- $tags = wp_get_post_tags($post->ID);
- foreach ($tags as $tag ) {
- $keywordarray[] = $tag->name;
- }
- $keywords = implode(',',array_unique((array)$keywordarray));
- else {
- $keywords = 'WordPress,WordPress 主題,WordPress 教程,WordPress 主題下載,WordPress 網站主題,WordPress 企業主題,WordPress 主題定製';
- $description = '主題貓,致力於為廣大網友提供最新最全的 WordPress 主題';
- }
- $output .= '<meta name="keywords" content="' . $keywords . '" />' . "n";
- $output .= '<meta name="description" content="' . $description . '" />' . "n";
- echo "$outputn";
- }
$1',$str);
第 43 行與第 44 行的內容需要根據你的網站進行修改。