WordPress 的每篇文章的评论,如果评论者填写了网址,那么在显示的时候就会加上这个连接,实际上是不利于优化的,分散了网站的权重,那么 WordPress 中如何更改评论者链接为站内链接。
在 functions.php 文件加入以下代码
- add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
- add_filter('comment_text', 'add_redirect_comment_link', 99);
- function add_redirect_comment_link($text = ''){
- $text=str_replace('href="', 'href="'.get_option('home').'/?r=', $text);
- $text=str_replace("href='", "href='".get_option('home')."/?r=", $text);
- return $text;
- }
- add_action('init', 'redirect_comment_link');
- function redirect_comment_link(){
- $redirect = $_GET['r'];
- if($redirect){
- if(strpos($_SERVER['HTTP_REFERER'], get_option('home')) !== false){
- header("Location: $redirect");
- exit;
- }else {
- header("Location: http://www.cdsy.com.cn");
- exit;
- }
- }
- }
在网站根目录下的 robots.txt 文件中添加以下代码,用来屏蔽搜索引擎收录重定向后的网址
- Disallow:/?r=*
再来看看前台评论者的链接吧,都变成如下形式了
- http://www.cdsy.com.cn/?r=http://www.cdsy.com.cn
但如果你的博客使用的是多说插件,那以上所有写的东西都是废话,没办法对多说里的评论起作用。
找到 WordPress 的 functions.php,然后在其中添加以下代码,保存上传即可。
- //评论加Nofollow
- function add_nofollow_to_comments_popup_link(){
- return ' rel="nofollow" ';
- }
- add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');
顺便优化以下,要加的地方有两个,一个是评论者的链接,还有就是那个 回复 这也是个链接。
- //为评论者添加nofollow属性
- function add_nofollow_to_comments_popup_link(){
- return 'rel="nofollow" ';
- }
- add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');
- //为评论回复链接加nofollow属性
- add_filter('comment_reply_link', 'add_nofollow_to_replay_link');
- function add_nofollow_to_replay_link( $link ){
- return str_replace( '")'>', '")' rel='nofollow'>', $link );
- }