Canonical Tag Issue on WordPress Pagination with Elementor and Yoast SEO Plugin

Recently, while working on a client’s website, we encountered an issue where the canonical tag generated by the Yoast SEO plugin conflicted with the theme code, resulting in an incorrect canonical URL structure.

How the Issue was found

Upon inspecting the source code of the paginated news pages, we noticed an issue with the canonical tag. Regardless of the page number, the canonical tag consistently points to the main news page URL, This is inconsistent and could potentially lead to problems with search engine indexing and diluted SEO efforts.


To solve this problem, we explored WordPress theme files and used PHP functions to obtain precise control over the canonical tag creation. First, we deactivated the canonical tag output from the Yoast SEO plugin to avoid conflicting instructionsNext, we created a custom PHP function that dynamically generates the canonical URL based on the current page’s pagination state.

In the theme’s functions.php file, we introduced a custom function named add_custom_canonical(). This function utilizes conditional statements to determine if the current page corresponds to the news archive or a paginated news page. It then calculates the correct canonical URL, considering the pagination state, and echoes the canonical tag with the appropriate URL.

// Disable Yoast SEO canonical tag add_filter('wpseo_canonical', '__return_false'); // Add custom canonical tag function add_custom_canonical() { if (is_page('news') || is_post_type_archive('news')) { global $paged; if (get_query_var('paged')) { $paged = get_query_var('paged'); } elseif (get_query_var('page')) { $paged = get_query_var('page'); } else { $paged = 1; } $canonical_url = get_permalink(); if ($paged > 1) { $canonical_url = trailingslashit($canonical_url) . $paged . '/'; } echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />'; } } add_action('wp_head', 'add_custom_canonical', 1);

All the above code must be added to the file functions.php

Leave a Comment

Your email address will not be published. Required fields are marked *