nofollow是HTML页面中a标签的属性值。它的用处是告诉搜索引擎”不要追踪此网页上的链接”或”不要追踪此特定链接”,也就是不会导出权重到此链接。
这对于WordPress进行SEO优化有着一定重要性。所以在文章内如果有外链地址,链接地址添加nofollw是很有必要的。
将下面的代码添加到主题的functions.php文件中:
// 自动给文章外链添加nofollow
function 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.'" rel="external nofollow" rel="external nofollow" ', 'href="'.$val.'" rel="external nofollow" rel="external nofollow" rel="external nofollow" ', $content);
}
}
}
return $content;
}
add_filter('the_content', 'the_content_nofollow', 200);