Learning never exhausts the mind

Published on by

Wordpress 2.7 introduced paged comments out 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 comments.

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 index 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 any 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.

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

Its 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
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" />';
}
?>

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.