Web Design that taps into the haromny and vision of your dreams.

How to Determine Paged Comments in WordPress 2.7

WordPress Paged Comments

By on in Coding

274 words, estimated reading time 2 minutes.

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()) && (!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" />';
}
?>

Last updated on: Thursday 22nd June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.