做seo的大家都知道如果文章中過多的外鏈會分散了該頁面的權重,這樣是不利于網站排名的。
所以網站內頁最好是盡量不要鏈接向外部。但是在某些情況下我們不得不鏈接向外部,那么該如何處理呢?其實我們可以給外部鏈接加上nofollow屬性,對蜘蛛聲明不要爬取這條鏈接。這樣就可以有效的解決權重流失的問題。
但是在編輯文章中一條鏈接一條鏈接的加nofollow屬性工作量實在太大了,小V就來教大家讓wordpress給文章中的外鏈自動添加nofollow屬性。
- add_filter( 'the_content', 'seo_wl'); function seo_wl( $content ) { $regexp = "]*href=("??)([^" >]*?)1[^>]*>"; if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { if( !emptyempty($matches) ) { $srcUrl = get_option('siteurl'); for ($i=0; $i < count($matches); $i++) { $tag = $matches[$i][0]; $tag2 = $matches[$i][0]; $url = $matches[$i][0]; $noFollow = ''; $pattern = '/targets*=s*"s*_blanks*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rels*=s*"s*[n|d]ofollows*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>'); $tag .= $noFollow.'>'; $content = str_replace($tag2,$tag,$content); } } } } $content = str_replace(']]>', ']]>', $content);
- return $content; } //Vevb.com
將以上代碼加入到當前主題的functions.php文件即可實現,換主題的時候記得把這段代碼加到新主題里噢,不然換主題后文章中的外部鏈接就會變成無nofollow屬性的了.
|
新聞熱點
疑難解答
圖片精選