以下代碼實(shí)現(xiàn)的是以標(biāo)簽為關(guān)鍵詞;以摘要為描述,如果沒有填寫摘要,那就自動截取文章前200字為描述。(教程轉(zhuǎn)自:DeveWork)
代碼實(shí)現(xiàn)WordPress自動關(guān)鍵詞與描述:
以下代碼放到你的主題下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',
- '$1',$str);
- }
- 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企業(yè)主題,wordpress主題定制';?
- $description?=?'主題貓,致力于為廣大網(wǎng)友提供最新最全的wordpress主題';
- }
- $output?.=?'<meta?name="keywords"?content="'?.?$keywords?.?'"?/>'?.?"n";
- $output?.=?'<meta?name="description"?content="'?.?$description?.?'"?/>'?.?"n";
- echo?"$outputn";
- }
- add_action('wp_head',?'meta_SEO');
第43行與第44行的內(nèi)容需要根據(jù)你的網(wǎng)站進(jìn)行修改。