以下代碼實現的是以標籤爲關鍵詞;以摘要爲描述,如果沒有填寫摘要,那就自動截取文章前200字爲描述。代碼原創者未知,如果是你原創的,麻煩告知~~
代碼實現WordPress自動關鍵詞與描述
以下代碼放到你的主題下funtions.php的最後一個 ?>前:
//自動關鍵詞與描述 Devework.com function get_cats_name() { $allcats=get_categories(); foreach ($allcats as $category) { $keywords[] = $category->cat_name; } return $keywords; } // utf8 substr 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); } // Meta SEO function meta_SEO() { global $post; $output = ''; if (is_single()){//如果是文章頁 $keywords = ''; $description = ''; if ($post->post_excerpt) {//如果文章摘要存在就以文章摘要爲描述 $description = $post->post_excerpt; $description = str_replace("\r\n","",$description); $description = str_replace("\n","",$description); $description = str_replace("\"","'",$description); $description .= '...'; } else {//如果文章摘要不存在就截斷文章前200字爲描述 $description = utf8Substr(strip_tags($post->post_content),0,200); $description = str_replace("\r\n","",$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,系統操作,網絡應用,軟件硬件,IT資訊,windows8,windows7'; //在引號間寫入你博客的關鍵字用,斷開 $description = '探討電腦知識,分享網絡資源';//在引號間寫入你博客的簡單描述,不要過200字 } //輸出關鍵字 $output .= '<meta name="keywords" content="' . $keywords . '" />' . "\n"; $output .= '<meta name="description" content="' . $description . '" />' . "\n"; //輸出描述 echo "$output\n"; } add_action('wp_head', 'meta_SEO');//添加meta_SEO函數到頭部信息裏
第43行與第44行的內容需要根據你的網站進行修改。
目前本站devework.com正在使用的代碼:
在這個上面代碼的基礎上,我的代碼是這樣的:
//自動關鍵詞與描述 Devework.com function meta_SEO() { global $post; $output = ''; if (is_single()){//如果是文章頁 $keywords = ''; $description = ''; if ($post->post_excerpt) {//如果文章摘要存在就以文章摘要爲描述 $description = $post->post_excerpt; $description = str_replace("\r\n","",$description); $description = str_replace("\n","",$description); $description = str_replace("\"","'",$description); $description .= '...'; } else {//如果文章摘要不存在就截斷文章前200字爲描述 $description = utf8Substr(strip_tags($post->post_content),0,200); $description = str_replace("\r\n","",$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 if (is_category()){ $description = strip_tags(trim(category_description())); $keywords = single_cat_title('', false); }else {//如果不是文章頁、分類頁 $keywords = 'WordPress,wordpress主題,wordpress插件,WordPress開發,代碼,前端,建站'; //在引號間寫入你博客的關鍵字用,斷開 $description = '一個有關WordPress技巧與前端開發知識的個人博客,以分享、研究探討WordPress技巧爲主要內容,博主乃WordPress極客一枚';//在引號間寫入你博客的簡單描述,不要過200字 } //輸出關鍵字 $output .= '<meta name="keywords" content="' . $keywords . '" />' . "\n"; $output .= '<meta name="description" content="' . $description . '" />' . "\n"; //輸出描述 echo "$output"; }
代碼還是放到你的主題下funtions.php的最後一個 ?>前。我的話爲分類目錄添加以“目錄名”爲關鍵詞,“分類描述”爲描述,因爲爲每一個分類都寫了一個描述(在後臺文章-分類目錄那裏可以寫)。而且在刪除了最後一句add_action('wp_head', 'meta_SEO');//添加meta_SEO函數到頭部信息裏而直接在header.php文件的下面直接加上下面一句引用:
<?php echo meta_SEO(); ?>
如果使用這個加上《代碼重寫WordPress網頁標題爲“原網頁標題|網站名”的形式》的方法的話,基本上你可以不用 All in one seo插件啦~