wordpress無插件實現(xiàn)給鏈接添加nofollow屬性

網站建設 SEO相關1 3,110閱讀模式

以前都是用插件實現(xiàn)的,但是用插件太占用服務器資源,google了一下,發(fā)現(xiàn)別人都總結好了,直接貼出來

[codesyntax lang="php"]

//給標簽云里的鏈接加上 rel="nofollow"
add_filter('wp_tag_cloud', 'cis_nofollow_tag_cloud');
function cis_nofollow_tag_cloud($text) {
return str_replace('<a href=', '<a rel="nofollow" href=', $text);
}

//給 the_tags() 生成的鏈接 加上 rel="nofollow"
add_filter('the_tags', 'cis_nofollow_the_tag');
function cis_nofollow_the_tag($text) {
return str_replace('rel="tag"', 'rel="tag nofollow"', $text);
}

//給 wp_list_categories() 生成的鏈接加上 rel="nofollow"
add_filter( 'wp_list_categories', 'cis_nofollow_wp_list_categories' );
function cis_nofollow_wp_list_categories( $text ) {

$text = stripslashes($text);
$text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text);
return $text;
}

//給 the_category() 生成的鏈接加上 rel="nofollow"
add_filter( 'the_category', 'cis_nofollow_the_category' );
function cis_nofollow_the_category( $text ) {

$text = str_replace('rel="category tag"', "", $text);
$text = cis_nofollow_wp_list_categories($text);
return $text;
}

//給 the_author_post_link 生成的鏈接加上 rel="nofollow"
add_filter('the_author_posts_link', 'cis_nofollow_the_author_posts_link');
function cis_nofollow_the_author_posts_link ($link) {
return str_replace('</a><a href=', '<a rel="nofollow" href=', $link);
}

//給 comments_popup_link_attributes() 生成的鏈接加上 rel="nofollow"
add_filter('comments_popup_link_attributes', 'cis_nofollow_comments_popup_link_attributes');
function cis_nofollow_comments_popup_link_attributes () {
echo ' rel="nofollow"';
}
//給外部鏈接添加nofollow屬性
add_filter('the_content','web589_the_content_nofollow',999);
function web589_the_content_nofollow($content){
	preg_match_all('/href="(.*?)" rel="external nofollow" /',$content,$matches);
	if($matches){
		foreach($matches[1] as $val){
			if( strpos($val,home_url())===false ) $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\" ",$content);
		}
	}
	return $content;
}

[/codesyntax]


如何給wordpress站內文章連接加上nofollow屬性

我們可以看到,很多人的博客內文章里的鏈接都是直接的純文本,這樣可以有效的組織權重的流散,原本是很不在意這個的,關于這個文章,是因為百度不收錄新文章的地步了,所以決定將站內的文章里的鏈接全部加上nofollow的標簽來阻止權重的流失。

不過顯然每個文章里面都通過手動來加上nofollow標簽,是我們這些懶人根本不愿干的事情。WordPress 默認的超鏈接里是沒有nofollow標簽的,所以我們需要給他增加一個這個功能。

在網上找了幾個方法,發(fā)現(xiàn)下面這個方法最快最省事。

首先,將以下代碼添加到functions.php 文件中

add_filter('the_content', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/]+/', 'my_nofollow_callback', $content);
}

function my_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');

if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}

至于放在哪里,只要不是把原來一段代碼切斷即可,你可以放在標注的前后。

這個代碼可以將所有文章內的站外鏈接全部加上nofollow屬性,而站內鏈接則不限制,而且已發(fā)布的文章頁有效。


 

 
  • 本文由 米粒在線 發(fā)表于 2013年1月14日19:31:54
  • 轉載請務必保留本文鏈接:http://m.bjmhhq.com/3155.html
網站建設

/var/log/maillog日志太大占滿/var空間如何清理

一、情景描述 節(jié)點磁盤空間告警,使用df -h查看發(fā)現(xiàn)是/var盤滿了,cd進入/var目錄下,du -sch log/ 發(fā)現(xiàn)是log目錄占用空間大,cd切換到log目錄下,du -dh *查看是因...
    • 189免費空間域名 0

      呵呵!我也是使用只更鳥的主題,

    發(fā)表評論

    匿名網友
    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
    確定

    拖動滑塊以完成驗證