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 TrottWordPress • January 24, 2009
How to Determine Paged Comments in WordPress

WordPress 2.7 introduced paged comments out of the box. However, you may want to block search engines from indexing these pages to prevent duplicate content. You may also 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. This can potentially harm your website's SEO. However, by preventing search engines from indexing these pages using the 'robots=nofollow' meta attribute, you can take control and avoid falling foul of the duplicate content filter. This simple function to identify a comment page and a little change to the header can save you from potential penalties by Google in the forthcoming Panda update.

The function for comment paged detection is straightforward and should be placed in the functions.php in your themes folder. It's a simple check that looks for 'comment-page' in the URL, making it easy for you to implement and manage.

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;
  }
} 

Implementing the 'comment-page' detection in the URL is straightforward. It's a simple check that looks for 'comment-page' in the URL.

In your header, you must add (or modify if you have a similar section) this code. This code is crucial as it prevents the indexing of paged comments. I have added the above function call on a different line for clarity, but you can 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" />';
}
?>

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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?

New comments for this post are currently closed.