Post Pic

WordPress hack: Canonical links for comments

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!

24 Responses

Aug 05 2009 18:23

I didn’t know what rel=canonical was, so I had to look it up. Here’s a great — and witty — quickie explanation by Conversation Marketing: http://www.conversationmarketing.com/2009/02/3-reasons-to-use-rel-canonical.htm

Aug 10 2009 01:48

Hi,
I landed in your blog while searching for “Canonical tag”.
I used it in my websites but had no idea how to imlement it in WP.
Finally I got the function. Thanks a lot.

Aug 14 2009 06:22

Hi, I think All in one SEO pack plugin and Platinum SEO pack can handle it.

Aug 18 2009 04:50

haha, nice i was looking for this.

great post and keep up the good work.

Aug 21 2009 04:14

@ Rismaka : maybe that for someone who didn’t use that plugin

Aug 27 2009 11:36

Yeah, its sucks when people steel content.

Sep 13 2009 20:23

Wow. This wonderful.

Oct 01 2009 17:21

for what canoncial is ?

Apr 09 2010 16:37

Hm, I tried your snippet but it didn’t work… :(

Aug 21 2010 11:16

Thanks for the code.
Actually I already had canonicals in place. I just hoped this snippet would maybe take in consideration the comment page (for those who have paged comments) you´re on. At the moment the post url and and all comment sub-pages still share the same canonical url which I´m sure is still a duplicate content issue. Is there a way to include the sub-comment page into the canonical tag?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required