最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Noindex Posts From Certain Authors In Wordpress

programmeradmin1浏览0评论

I'm trying to noindex posts from all authors EXCEPT 3 authors on my Wordpress site.

I found the following code that I can put into the header.php file. However, this targets specific categories.

<?php if (is_single() && (in_category(array(457))))  { 
echo '<meta name="robots" content="noindex, follow">';
} ?>

How do I modify this to say something like, if is single post and is NOT an author with ID 111, 112, or 113 then insert noindex, follow. Would the following be correct?:

 <?php if (is_single() && !(is_author(array(111,112,113))))  { 
    echo '<meta name="robots" content="noindex, follow">';
    } ?>

I'm trying to noindex posts from all authors EXCEPT 3 authors on my Wordpress site.

I found the following code that I can put into the header.php file. However, this targets specific categories.

<?php if (is_single() && (in_category(array(457))))  { 
echo '<meta name="robots" content="noindex, follow">';
} ?>

How do I modify this to say something like, if is single post and is NOT an author with ID 111, 112, or 113 then insert noindex, follow. Would the following be correct?:

 <?php if (is_single() && !(is_author(array(111,112,113))))  { 
    echo '<meta name="robots" content="noindex, follow">';
    } ?>
Share Improve this question asked Feb 19, 2020 at 18:44 TnaceTnace 173 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

From codex: is_author() is a conditional tag which determines whether the query is for an existing author archive page. so it does not work for your scope.

Best solution, instead of using the template file header.php is to write a function in functions.php hooking the proper action wp_head:

add_action('wp_head','AS_exclude_author_from_indexing');

function AS_exclude_author_from_indexing(){
  $toIndex = array(111,112,113);
  $user_id = get_the_author_meta( 'ID' );
  if( !in_array($user_id,$toIndex)){
    echo "<meta name=\"robots\" content=\"noindex,follow\">".PHP_EOL;
  } 
}
发布评论

评论列表(0)

  1. 暂无评论