本文实例讲述了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属性,那你能够将上面的代码增加到主题的 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属性的实现方法》的一些看法。更多内容请查看本栏目更多内容!
这篇文章重点引见10个重要的WordPress平安插件和技巧,用来维护WordPress网站或许博客。1. WP Security人工...
关于日拜访量还不错的WordPress网站来说,一定都会存在很多待审核的渣滓评论,假如要经过WP后盾删除,那几乎...
WordPress 的自定义字段就是文章的 meta 信息(元信息),利用这个性能,能够扩大文章的性能,是学习 WordP...
现在,WordPress CMS 主题越来越多,WordPress 作为 CMS 终究有那些共同的劣势,吸援用户去一直的延长扩大呢...
要害字形容:罕用 插件 代码 以及 &mdash 文章 即可   而后 统计 这篇文章是我之前为一个香港人...
共性化登陆界面的办法有两种,第一种是交换登陆背景,这个算是小型共性化吧: 在/wp-admin/images找到login...