WordPress官方下載安裝的程序,只能填寫網站標題和副標題,通過插件來添加卻又擔心會影響網站的整體使用出錯,關鍵詞(keywords)和描述(description)用代碼添加的方法如下:
第一步、進入wordpress后臺,找到“外觀”模塊下面 的“編輯”選項,進入主題編輯選項;在模版里面點擊“頂部(header.php)”模版如圖:
第二步、在左側的header編輯框中找到
第三步、在
<?php if (is_home()){ //這里描述在前******* $description = "在此輸入描述"; $keywords = "在此輸入關鍵詞"; } elseif (is_category()){ $keywords = single_cat_title('', false); $description = category_description(); } elseif (is_tag()){ $keywords = single_tag_title('', false); $description = tag_description(); } $keywords = trim(strip_tags($keywords)); $description = trim(strip_tags($description)); ?><meta>"?/> <meta>"?/>
第四步,前三步是設置了主頁的關鍵詞描述,如果還要頁面和文章的關鍵詞(keywords)和描述(description),上面的代碼這全部更換,使用以下代碼:
<?php //如果是首頁 if (is_home()){ $keywords = "你網站首頁的關鍵字,自己修改吧"; $description = "你網站首頁的描述,自己修改吧";} //如果是文章頁 elseif (is_single()){ //默認使用文章頁添加關鍵字 $keywords = get_post_meta($post->ID,?"keywords",?true);???????? ?????//如果為空,使用標簽作為關鍵字 if($keywords?==?""){? ???$tags?=?wp_get_post_tags($post->ID); ???foreach?($tags?as?$tag){ ???$keywords?=?$keywords.$tag->name.",";}? ?????//去掉最后一個, ?????$keywords?=?rtrim($keywords,?',?');???????? }????????//默認使用文章頁添加描述 $description?=?get_post_meta($post->ID,?"description",?true); ????????//如果為空,使用文章前100個字作為描述 if($description?==?""){? ???????????????if($post->post_excerpt){? ??????????????????$description?=?$post->post_excerpt;???? ?????}else{ ???????????$description?=?mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200); ????????????}????????}} //如果是頁面,使用頁面添加的關鍵字和描述 elseif?(is_page()){ ????????$keywords?=?get_post_meta($post->ID,?"keywords",?true); ????????$description?=?get_post_meta($post->ID,?"description",?true);} //如果是分類頁,使用分類名作為關鍵字,分類描述作為描述 elseif?(is_category()){ ????????$keywords?=?single_cat_title('',?false); ????????$description?=?category_description();} //如果是標簽頁,使用標簽名作為關鍵字,標簽描述作為描述 elseif?(is_tag()){ ????????$keywords?=?single_tag_title('',?false);???????? ????????$description?=?tag_description();} //去掉兩段空格 ????????$keywords?=?trim(strip_tags($keywords)); ????????$description?=?trim(strip_tags($description));?> <meta>"?/> <meta>"?/>
第五步、下面這段一定要添加在前面就可以了,文章頁和頁面都可以自動添加了
<meta>"?/> <meta>"?/>
更多wordpress相關技術文章,請訪問wordpress教程欄目進行學習!
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END