Using WordPress ‘rel_canonical()’ PHP function

The rel_canonical WordPress PHP function outputs the rel=”canonical” link tag for singular queries on your website.

Usage

rel_canonical();

Parameters

  • None

More information

See WordPress Developer Resources: rel_canonical

Examples

Add rel=”canonical” to singular post templates

If you want to add the rel=”canonical” tag to your single post templates, add the following code to your theme’s header.php file within the <head> tag:

if (is_singular()) {
  rel_canonical();
}

Add rel=”canonical” to custom post type templates

To add the rel=”canonical” tag to custom post type templates, add the following code to your theme’s header.php file within the <head> tag:

if (is_singular('your_custom_post_type')) {
  rel_canonical();
}

Add rel=”canonical” to custom taxonomy templates

To add the rel=”canonical” tag to custom taxonomy templates, add the following code to your theme’s header.php file within the <head> tag:

if (is_tax('your_custom_taxonomy')) {
  rel_canonical();
}

Add rel=”canonical” to specific page templates

To add the rel=”canonical” tag to specific page templates, add the following code to your theme’s header.php file within the <head> tag:

if (is_page_template('your_page_template.php')) {
  rel_canonical();
}

Add rel=”canonical” to attachment templates

To add the rel=”canonical” tag to attachment templates, add the following code to your theme’s header.php file within the <head> tag:

if (is_attachment()) {
  rel_canonical();
}