Hello,
Thank you for contacting Rank Math for help setting the main category page as canonical for paginated category listings.
To set the canonical pagination page, please add the following function to your (child) theme functions.php file:
add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
if ( is_paged() ) {
$link = $_SERVER['REQUEST_URI'];
$arr = explode('/p/', $link,);
$canonical = home_url($arr[0]. '/');
}
return $canonical;
});
Hope that helps. Please let us know if you have questions.
Unfortunately, this code does not work.
It just adds an extra / :
With code:
URL /shop/page/2/.
Canonical /shop/page/2//.
What’s needed is:
URL /shop/page/2/.
Canonical /shop/
Hello,
Please replace the previous code with the below one and see if it helps:
add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
if ( is_paged() ) {
$link = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$arr = explode('/page/', $link);
$canonical = $arr[0] . '/';
}
return $canonical;
});
Let us know how that goes.
Thank you.
Yes, this code works correctly. Thank you very much!
Hello,
We are super happy that we could address your concern. If you have any other questions in the future, know that we are here to help you.
If you don’t mind us asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.
Hello,
Thank you so much for leaving us a review. We really appreciate it.
If you have any further questions, please feel free to start a new forum discussion, and it will be our pleasure to assist you once more.
Thank you.