WordPress自动给文章添加nofollow属性的实现方法
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>本文实例讲述了WordPress自动给文章添加nofollow属性的实现方法。分享给大家供大家参考。具体分析如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
nofollow属性是告诉搜索引擎不传权重过去,但WordPressk中如果我们要nofollow属性就需要手工加了,现在我来告诉大家利用 Nofollow for external link就可以自动给文章添加nofollow属性了.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
直接安装启用 Nofollow for external link 插件,或者将下面的代码添加到当前主题的 functions.php 文件即可.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
实例代码如下:<br>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode4" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
add_filter( 'the_content', 'cn_nf_url_parse'); <br><br>
function cn_nf_url_parse( $content ) { <br><br>
$regexp = "<as[^>]*href=("??)([^" >]*?)\1[^>]*>"; <br>
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) { <br>
if( !emptyempty($matches) ) { <br>
$srcUrl = get_option('siteurl'); <br>
for ($i=0; $i < count($matches); $i++) <br>
{ <br>
$tag = $matches[$i]; <br>
$tag2 = $matches[$i]; <br>
$url = $matches[$i]; <br>
$noFollow = ''; <br>
$pattern = '/targets*=s*"s*_blanks*"/'; <br>
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); <br>
if( count($match) < 1 ) <br>
$noFollow .= ' target="_blank" '; <br>
$pattern = '/rels*=s*"s*ofollows*"/'; <br>
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE); <br>
if( count($match) < 1 ) <br>
$noFollow .= ' rel="nofollow" '; <br><br>
$pos = strpos($url,$srcUrl); <br>
if ($pos === false) { <br>
$tag = rtrim ($tag,'>'); <br>
$tag .= $noFollow.'>'; <br>
$content = str_replace($tag2,$tag,$content); <br>
} <br>
} <br>
} <br>
} <br>
$content = str_replace(']]>', ']]>', $content); <br>
return $content; <br>
}</div>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>最终效果:自动给文章/页面的站外链接添加nofollow属性(rel=”nofollow”),并且在新窗口打开这些链接(即添加 target=”_blank”属性),如果已经手动给链接添加了 rel=”dofollow”,就不会添加 rel=”nofollow”,如果手动添加了 target=”_blank”,就不会重复添加.</span>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
为指定分类的所有链接添加nofollow属性,那你可以将下面的代码添加到主题的 functions.php 文件即可:<br>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode5" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
function nofollow_cat_posts($text) { <br>
global $post; <br>
if( in_category(1) ) { // 修改这里的分类ID <br>
$text = stripslashes(wp_rel_nofollow($text)); <br>
} <br>
return $text; <br>
} <br>
add_filter('the_content', 'nofollow_cat_posts');</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家的WordPress建站有所帮助。</p>
頁:
[1]