How do I add canonical tags to WordPress category/archive pages?

by chasity.halvorson , in category: SEO , 2 years ago

How do I add canonical tags to WordPress category/archive pages?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

2 answers

by percy_bauch , 2 years ago

@chasity.halvorson 

There are several ways to add canonical tags to WordPress category/archive pages:

  1. Using a plugin: One of the easiest ways to add canonical tags to WordPress is by using a plugin such as Yoast SEO or All in One SEO Pack. Simply install the plugin, and then navigate to the plugin's settings. From there, you can configure the canonical tags for your category and archive pages.
  2. Manually adding the code: You can also manually add the code for the canonical tags to the header.php file of your WordPress theme. To do this, open the header.php file and paste the following code just before the closing </head> tag:
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. Using a function: If you prefer to use a function, you can add the following code to your theme's functions.php file:
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.

Member

by mabelle , a year ago

@chasity.halvorson 

It is important to note that before making any changes to your theme files or adding any code, it is recommended to create a backup of your website.


Also, consider the following:

  1. Using a plugin: This is the easiest method, especially for users who are not comfortable manually editing theme files. Install and activate a reputable SEO plugin like Yoast SEO or All in One SEO Pack. These plugins offer built-in functionality to add canonical tags to category and archive pages.
  2. Manually editing the header.php file: Access your WordPress installation via FTP or through your web hosting control panel. Locate the header.php file within your currently active theme's folder. Open the file using a text editor (such as Notepad++) and add the following code just before the closing tag:
1
2
3
<?php if (is_category()) { ?>
<link rel="canonical" href="<?php echo get_permalink( $post->post_parent ); ?>" />
<?php } ?>


Save the changes and re-upload the edited header.php file to your server.

  1. Adding a function to functions.php: This method involves adding a custom function to your theme's functions.php file. Access your WordPress installation via FTP or through your web hosting control panel. Locate the functions.php file within your currently active theme's folder. Open the file using a text editor and add the following code:
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');


Save the changes and re-upload the updated functions.php file to your server.


Remember to clear any caching plugins or server caching after making these changes to ensure the canonical tags are being displayed correctly.


It is also worth mentioning that canonical tags are essential for avoiding duplicate content issues and providing search engines with a preferred version of your content. Make sure to set the canonical URL appropriately to avoid any negative impact on your website's SEO.