How do I add canonical tags to WordPress category/archive pages?
@chasity.halvorson
There are several ways to add canonical tags to WordPress category/archive pages:
1 2 3 |
<?php if (is_category()) { ?> <link rel="canonical" href="<?php echo get_permalink( $post->post_parent ); ?>" /> <?php } ?> |
This code will set the canonical tag for your category pages to the parent page.
1 2 3 4 5 6 |
function add_canonical_tags() { if (is_category()) { echo '<link rel="canonical" href="' . get_permalink( $post->post_parent ) . '" />'; } } add_action('wp_head', 'add_canonical_tags'); |
This code will set the canonical tag for your category pages to the parent page.
Whichever method you choose, be sure to test your website after making the changes to make sure that the canonical tags are being added correctly.