WordPress主动给文章增加nofollow属性的完成办法

本文实例讲述了WordPress主动给文章增加nofollow属性的完成办法。分享给大家供大家参考。详细剖析如下:

nofollow属性是通知搜寻引擎不传权重过来,但WordPressk中假如咱们要nofollow属性就需求手工加了,如今我来通知大家利用 Nofollow for external link就能够主动给文章增加nofollow属性了.

间接装置启用 Nofollow for external link 插件,或许将上面的代码增加到以后主题的 functions.php 文件即可.

实例代码如下:

add_filter( 'the_content', 'cn_nf_url_parse');  

function cn_nf_url_parse( $content ) {

$regexp = "<as[^>]*href=("??)([^" >]*?)\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !emptyempty($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;
}

最终成果:主动给文章/页面的站外链接增加nofollow属性(rel=”nofollow”),并且在新窗口关上这些链接(即增加 target=”_blank”属性),假如曾经手动给链接增加了 rel=”dofollow”,就不会增加 rel=”nofollow”,假如手动增加了 target=”_blank”,就不会反复增加.

为指定分类的一切链接增加nofollow属性,那你能够将上面的代码增加到主题的 functions.php 文件即可:

function nofollow_cat_posts($text) {  
global $post;
if( in_category(1) ) { // 修正这里的分类ID
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_cat_posts');

心愿本文所述对大家的WordPress建站有所协助。

以上就是安达网络工作室关于《WordPress自动给文章添加nofollow属性的实现方法》的一些看法。更多内容请查看本栏目更多内容!

版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
分享一些适用WordPress SQL查问语句

以下代码来自网络,未经测试,操作数据库有危险,请事前备份 !为一切文章和页面增加自定义字段 这段代码能...

wordpress设置友谊链接只在首页显示的办法

关于有的主题,当从后盾小工具中增加友谊链接后,却发现其是为全站显示的。假如只心愿让它在首页显示,则能...

编写PHP脚原本完成WordPress中评论分页的性能

办法阐明 首先来看看可能被用到的办法. 关上文件 wp-includes/link-template.php 你会发现 WordPress 2.7 多...

优化WordPress的Google字体以减速国际效劳器上的运转

因为Google被彻底墙掉,不翻墙就无奈加载谷歌字体。很多老手冤家常常埋怨本人买的主机如何如何慢,给的示例...

深化解析WordPress中加载模板的get_template_part函数

最近钻研民间主题 Twenty Eleven ,有一些货色网上现成的中文材料不好找,在博客里记录上去,算是分享,也算...

wordpress定时义务(wp-cron.php)造成主机CPU比拟高的处理方法

自从用了阿里云主机,真是没有省心过。 这两天网站CPU天天100+的,过一会就挂了,原来是wp-cron.php文件造成...

需求提交

客服服务

亿鸽在线客服系统