Prevent Duplicate Content On Comment Pages

 Prevent Duplicate Content On Comment Pages

Sm7 in 10 Useful WordPress Hook Hacks

The problem.
Duplicate content is an SEO problem for many websites. It can also be a problem for your WordPress blog if you don’t apply a few tricks.

Introduced in version 2.7, paged comments is a great addition to WordPress because it lets bloggers split comments into multiple pages. The only problem, again, is the risk of duplicate content.

Let’s use the new rel="canonical" attribute to prevent duplicate content in paged comments.

The solution.
Simply paste the following code in your function.php file:

01 function canonical_for_comments() {
02     global $cpage$post;
03     if $cpage > 1 ) :
04         echo "\n";
05         echo "<link rel='canonical' href='";
06         echo get_permalink( $post->ID );
07         echo "' />\n";
08      endif;
09 }
11 add_action( 'wp_head''canonical_for_comments' );

Code explanation.
First, we create a function to add the rel="canonical" attribute to comment pages, except page 1. Then we hook this function to WordPress’ wp_head() function. As simple as that!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s