出于SEO方面的考慮,對于文章或評論內(nèi)容的站外鏈接,不少人都希望自動添加nofollow屬性,直接將下面的代碼添加到主題的 functions.php 文件即可:
add_filter('the_content', 'auto_nofollow'); //nofollow文章內(nèi)容的站外鏈接
add_filter('comment_text', 'auto_nofollow'); //nofollow評論內(nèi)容的站外鏈接
function auto_nofollow($content) {
//return stripslashes(wp_rel_nofollow($content));
return preg_replace_callback('/<a>]+/', 'auto_nofollow_callback', $content);
}
function auto_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;
}
代碼中的第1、3行分別是針對文章內(nèi)容、評論內(nèi)容的,請根據(jù)自己的需要選擇。比如不需要自動給文章內(nèi)容的站外鏈接添加 nofollow 的話,就注銷或刪除第一行代碼。
聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。





怎么給外鏈加上網(wǎng)站鏈接,比如http://www.ydqwiac.cn/source?=www.baidu.com,這樣子的,加上自己網(wǎng)站鏈接,需要什么代碼呢
拜讀,功能正好需要_(:з」∠)_
這個好,不然文章外鏈太多了