用代碼自動讓wordpress的文章連結,加入nofollow屬性

很多時候我在寫文章的時候,會使用一些外部連結
你可以會連外部的圖片、檔案。
或是你會貼別人的網址。
但在做seo的時候,會比較不利,因此我們可以使用nofollow屬性
讓搜尋引擎知道,不用去抓爬外部的連結。
可以使得你這篇文篇,容易被搜尋引擎所收錄。

使用以下代碼即可完成,開啟主題的functions.php [cc lang="php"] //用代碼自動讓wordpress的文章連結,加入nofollow屬性 add_filter( 'the_content', 'wailian_nofollow'); function wailian_nofollow( $content ) { $regexp = "<as[^>]*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 = '/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; } [/cc]

Social button for Joomla

評論 (0)

此處尚未發表評論

留言

  1. 以遊客身份發表評論。 註冊登入到您的帳戶。
附件 (0 / 3)
分享您的位置