WordPress hack: Canonical links for comments

August 3, 2009 at 9:00 am

Duplicate content is one of the most discussed topics in terms of SEO. Happilly, with the new rel=’canonical’ attribute, you can now reduce risks of duplicate content. Here is how to implement it for your comments pages, on your WordPress blog.

Simply paste the following code on your function.php file:

function canonical_for_comments() {
 global $cpage, $post;
 if ( $cpage > 1 ) :
  echo "\n";
  echo "<link rel='canonical' href='";
  echo get_permalink( $post->ID );
  echo "' />\n";
 endif;
}
add_action( 'wp_head', 'canonical_for_comments' );

Thanks to I like WordPress for the useful tip!