• Jan 11, 2023

wp_pagenavi add attribute to the pagination link

If you want to add a rel='canonical' attribute to the pagination links generated by the wp_pagenavi plugin, you can use the str_replace() function to replace the <a> tag with a new <a> tag that includes the rel='canonical'attribute.

Here’s an example of how you can do this:

function custom_wp_pagenavi( $html ) {
    $html = str_replace( '<a ', '<a rel="canonical" ', $html );
    return $html;
}
add_filter( 'wp_pagenavi', 'custom_wp_pagenavi' );

This code will replace all the '<a ' in the $html output, with '<a rel="canonical" ' adding the rel attribute, it will add the attribute to all the anchor tags in pagination links

It is important to note that this will only affect the pagination links generated by the wp_pagenavi plugin, not the main URL of the page.

The "rel=canonical" link element is used to tell search engines which version of the content is the original one. It is used to avoid duplicate content, for example, when you have a category page, pagination pages and search pages that have the same content.

Share on: