用代码自动让wordpress的文章连结,加入nofollow属性

很多时候我在写文章的时候,会使用一些外部连结
你可以会连外部的图片、档案。
或是你会贴别人的网址。
但在做seo的时候,会比较不利,因此我们可以使用nofollow属性
让搜寻引擎知道,不用去抓爬外部的连结。
可以使得你这篇文篇,容易被搜寻引擎所收录。

使用以下代码即可完成,开启主题的functions.php [cc lang="php"] //用代码自动让wordpress的文章连结,加入nofollow属性add_filter( 'the_content', 'wailian_nofollow'); function wailian_nofollow( $ content ) { $regexp = "]*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); retur n $content; } [/cc]

Social button for Joomla

评论 (0)

此处尚未发表评论

留言

  1. 以游客身份发表评论。注册登入到您的帐户。
附件 (0/ 3)
分享您的位置