A-A+
wordpress文章外链自动添加nofollow属性
给一个客户优化一个由 wordpress 做的采集站点,尼玛呀,外链好恐怖,全是原站点的链接,做SEO的小伙伴们都知道,一个页面的文章里面全是外链的话,会分散掉这个页面的权重,怪不得流量不上去呢,我的建议是直接去掉这些外链链接,但文章内容太多,有些链接又是需要的,看来直接去掉外链链接肯定是不行的了,哪就只能添加 nofollow 属性了,但一条条的进行编辑添加工作量又太大了。
还好夏日博客从互联网上找到这样的一段代码:
add_filter( 'the_content', 'xiariboke_seo_wl'); function xiariboke_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; }
只要将这段代码放入到主题 functions.php 文件中即可实现文章外链自动添加上 nofollow 属性了,从此再也不用担心文章页面传递过多权重的问题了,从而保住了页面的权重。
请问如何给自己回复的评论不加nofollow
给 A 标记添加上 nofollow 属性标记即可。