做SEO的大家都知道如果文章中過(guò)多的外鏈會(huì)分散了該頁(yè)面的權(quán)重,,這樣是不利于網(wǎng)站排名的。所以網(wǎng)站內(nèi)頁(yè)最好是盡量不要鏈接向外部,。
但是在某些情況下我們不得不鏈接向外部,,那么該如何處理呢?其實(shí)我們可以給外部鏈接加上nofollow屬性,對(duì)蜘蛛聲明不要爬取這條鏈接,。
這樣就可以有效的解決權(quán)重流失的問(wèn)題,。但是在編輯文章中一條鏈接一條鏈接的加nofollow屬性工作量實(shí)在太大了,下面就來(lái)教大家讓wordpress給文章中的外鏈自動(dòng)添加nofollow屬性,。
add_filter( 'the_content', 'zyku_seo_wl'); function zyku_seo_wl( $content ) { $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>"; if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { if( !empty($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 = '/target\s*=\s*"\s*_blank\s*"/'; preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); if( count($match) < 1 ) $noFollow .= ' target="_blank" '; $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/'; 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;}
將以上代碼加入到當(dāng)前主題的functions.php文件即可實(shí)現(xiàn),,換主題的時(shí)候記得把這段代碼加到新主題里噢,不然換主題后文章中的外部鏈接就會(huì)變成無(wú)nofollow屬性的了,。