How to Determine Paged Comments in WordPress

WordPress 2.7 added paged comments however you may want to prevent search engines from indexing extra pages to prevent duplicate content

By Tim Trott | WordPress | January 24, 2009

WordPress 2.7 introduced paged comments out of the box, however, you will probably be wanting to prevent search engines from indexing these pages to prevent duplicate content. You may also just wish to determine if a user is viewing a paged comment.

The new update to WordPress will allow your comments to be paged, that is show comments 1-50 on page 1, 51 - 100 on page 2 etc, but it will also have the effect of duplicating your content on many pages. It is common practice to exclude certain pages from being indexed by search engines by using the "robots=nofollow" meta attribute. With WordPress 2.7 if you have multiple comment pages, all pages will be indexed and you may fall foul of the duplicate content filter, and you could be heavily penalised by Google in the forthcoming Panda update. You can prevent this with a little function which will identify a comment page and a little change to the header.

The function for comment paged detection should be placed in the functions.php in your themes folder.

Detect Paged Comments in WordPress

php
function is_comments_paged()
{
  $pos = strpos($_SERVER['REQUEST_URI'], "comment-page");
  
  if ($pos === false) 
  {
    return false;
  } 
  else 
  {
    return true;
  }
} 

It's quite simple really, it just looks for 'comment-page' in the URL.

In your header, you will need to add (or modify if you have a similar section) this code. For clarity, I have added the above function call on a different line, but you can just merge it with the first if statement.

php
<?php
if((is_home() || is_single() || is_category() || is_page()) &amp;&amp; (!is_paged())) 
{
  if (is_comments_paged())
    echo '  <meta name="robots" content="noindex,follow" />';
  else
    echo '  <meta name="robots" content="index,follow" />';
} 
else 
{
  echo '  <meta name="robots" content="noindex,follow" />';
}
?>
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.