WordPress的文章自動(dòng)添加關(guān)鍵詞及關(guān)鍵詞的SEO優(yōu)化
網(wǎng)站的關(guān)鍵字及網(wǎng)頁描述關(guān)系網(wǎng)站對(duì)搜索引擎的友好程度,如果自己手動(dòng)加顯然太折騰了,那如何讓wordpress博客自動(dòng)為每篇文章自動(dòng)關(guān)鍵字及網(wǎng)頁描述。每篇文章的內(nèi)容不同,我們?cè)撊绾巫寃ordpress自動(dòng)添加文章描述和關(guān)鍵詞呢?下面就讓我們來看看如何給wordpress自動(dòng)添加文章描述和關(guān)鍵詞。
在你主題的functions.php文件添加以下代碼,各個(gè)代碼的功能解析如下:
add_action ( 'wp_head', 'wp_keywords' ); // 添加關(guān)鍵字 add_action ( 'wp_head', 'wp_description' ); // 添加頁面描述 function wp_keywords() { global $s, $post; $keywords = ''; if (is_single ()) { //如果是文章頁,關(guān)鍵詞則是:標(biāo)簽+分類ID if (get_the_tags ( $post->ID )) { foreach ( get_the_tags ( $post->ID ) as $tag ) $keywords .= $tag->name . ', '; } foreach ( get_the_category ( $post->ID ) as $category ) $keywords .= $category->cat_name . ', '; $keywords = substr_replace ( $keywords, '', - 2 ); } elseif (is_home ()) { $keywords = '我是主頁關(guān)鍵詞'; //主頁關(guān)鍵詞設(shè)置 } elseif (is_tag ()) { //標(biāo)簽頁關(guān)鍵詞設(shè)置 $keywords = single_tag_title ( '', false ); } elseif (is_category ()) {//分類頁關(guān)鍵詞設(shè)置 $keywords = single_cat_title ( '', false ); } elseif (is_search ()) {//搜索頁關(guān)鍵詞設(shè)置 $keywords = esc_html ( $s, 1 ); } else {//默認(rèn)頁關(guān)鍵詞設(shè)置 $keywords = trim ( wp_title ( '', false ) ); } if ($keywords) { //輸出關(guān)鍵詞 echo "<meta name=\"keywords\" content=\"$keywords\" />\n"; } } function wp_description() { global $s, $post; $description = ''; $blog_name = get_bloginfo ( 'name' ); if (is_singular ()) { //文章頁如果存在描述字段,則顯示描述,否則截取文章內(nèi)容 if (! empty ( $post->post_excerpt )) { $text = $post->post_excerpt; } else { $text = $post->post_content; } $description = trim ( str_replace ( array ( "\r\n", "\r", "\n", " ", " " ), " ", str_replace ( "\"", "'", strip_tags ( $text ) ) ) ); if (! ($description)) $description = $blog_name . "-" . trim ( wp_title ( '', false ) ); } elseif (is_home ()) {//首頁顯示描述設(shè)置 $description = $blog_name . "-" . get_bloginfo ( 'description' ) .'首頁要顯示的描述'; // 首頁要自己加 } elseif (is_tag ()) {//標(biāo)簽頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . single_tag_title ( '', false ) . "' 的文章"; } elseif (is_category ()) {//分類頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . single_cat_title ( '', false ) . "' 的文章"; } elseif (is_archive ()) {//文檔頁顯示描述設(shè)置 $description = $blog_name . "在: '" . trim ( wp_title ( '', false ) ) . "' 的文章"; } elseif (is_search ()) {//搜索頁顯示描述設(shè)置 $description = $blog_name . ": '" . esc_html ( $s, 1 ) . "' 的搜索結(jié)果"; } else {//默認(rèn)其他頁顯示描述設(shè)置 $description = $blog_name . "有關(guān) '" . trim ( wp_title ( '', false ) ) . "' 的文章"; } //輸出描述 $description = mb_substr ( $description, 0, 220, 'utf-8' ) . '..'; echo "<meta name=\"description\" content=\"$description\" />\n"; }
突出關(guān)鍵字在搜尋結(jié)果:
function wps_highlight_results($text){ if(is_search()){ $sr = get_query_var('s'); $keys = explode(" ",$sr); $text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong>'.$sr.'</strong>', $text); } return $text; } add_filter('the_excerpt', 'wps_highlight_results'); add_filter('the_title', 'wps_highlight_results');
使用此代碼段突出顯示搜索詞在你的博客搜索結(jié)果the_excerpt和the_title。
相關(guān)文章
php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法
這篇文章主要介紹了php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法,可利用memcache緩解服務(wù)器讀寫壓力,并實(shí)現(xiàn)數(shù)據(jù)庫數(shù)據(jù)的寫入操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12PHP中simplexml_load_string函數(shù)使用說明
這個(gè)問題遇到好幾次了,今天翻看以前代碼的時(shí)候看到,便記下來,需要的朋友可以參考下。2011-01-01php數(shù)組添加與刪除單元的常用函數(shù)實(shí)例分析
這篇文章主要介紹了php數(shù)組添加與刪除單元的常用函數(shù),實(shí)例分析了array_push、array_pop、array_shift及array_unshift等函數(shù)的使用技巧,需要的朋友可以參考下2015-02-02smarty靜態(tài)實(shí)驗(yàn)表明,網(wǎng)絡(luò)上是錯(cuò)的~呵呵
smarty靜態(tài)實(shí)驗(yàn)表明,網(wǎng)絡(luò)上是錯(cuò)的~呵呵...2006-11-11php json_encode值中大括號(hào)與花括號(hào)區(qū)別
這篇文章主要介紹了json_encode值中大括號(hào)與花括號(hào)區(qū)別,具體的看下實(shí)例說明,需要的朋友參考下2013-09-09php 將bmp圖片轉(zhuǎn)為jpg等其他任意格式的圖片
php 將bmp圖片轉(zhuǎn)為jpg等其他任意格式的圖片 ,這樣大家就不用為圖片是bmp格式的而發(fā)愁了。2009-06-06