免插件为WordPress文章中标签添加内链
生活随笔
收集整理的這篇文章主要介紹了
免插件为WordPress文章中标签添加内链
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給文章標簽添加內鏈,意思就是說,如果你文章中出現了和標簽一樣的文字,那么這個文字就會自動成為標簽鏈接,你點擊這個鏈接就會查看到所有含有該標簽的文章,這個能方便用戶瀏覽,據說還利于SEO。下面說說方法:
把下面的代碼添加到function.php中就可以了
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //自動為文章標簽添加該標簽的鏈接 $match_num_from = 1;? // 一個標簽在文章中出現少于多少次不添加鏈接 $match_num_to = 1; // 一篇文章中同一個標簽添加幾次鏈接 add_filter('the_content','tag_link',1); //按長度排序 function tag_sort($a, $b){ ????if ( $a->name == $b->name ) return 0; ????return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1; } //為符合條件的標簽添加鏈接 function tag_link($content){ ????global $match_num_from,$match_num_to; ????$posttags = get_the_tags(); ????if ($posttags) { ????????usort($posttags, "tag_sort"); ????????foreach($posttags as $tag) { ????????????$link = get_tag_link($tag->term_id); ????????????$keyword = $tag->name; ????????????//鏈接的代碼 ????????????$cleankeyword = stripslashes($keyword); ????????????$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\""; ????????????$url .= ' target="_blank"'; ????????????$url .= ">".addcslashes($cleankeyword, '$')."</a>"; ????????????$limit = rand($match_num_from,$match_num_to); ????????????//不鏈接的代碼 ????????????$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content); ????????????$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content); ????????????$cleankeyword = preg_quote($cleankeyword,'\''); ????????????$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case; ????????????$content = preg_replace($regEx,$url,$content,$limit); ????????????$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content); ????????} ????} ????return $content; } |
看看效果圖:
總結
以上是生活随笔為你收集整理的免插件为WordPress文章中标签添加内链的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给公司部门设计的SOA架构
- 下一篇: CSS语法总结