-
Hello there.
Rank Math free. Store 15k products.
At the end of construction. At this moment it is nonindex.Until the moment, I’m going to use RankMath pro, I would like a solution, please, to solve the part with the Canonical tag.
I’m currently testing this:
add_filter( 'rank_math/frontend/canonical', function( $canonical ) {
// change canonical URL for entire website
return $canonical;
});add_filter( "rank_math/frontend/next_rel_link", function( $link ) {
// change the next rel link for entire website
$link = '<link rel="next" href="'.get_next_posts_page_link().'" />';
return $link;
});add_filter( "rank_math/frontend/prev_rel_link", function( $link ) {
// change the prev rel link for entire website
$link = '<link rel="prev" href="'.get_previous_posts_page_link().'" />';
return $link;
});
However, in tests done in online tools, some say I have canonical, others don’t.
I’ve tried various things, but without ok results, like:
// create the canonical meta tag for the current page
function cano_add_canonical_link() {
global $post;
if( is_singular() ) {
echo '<link rel="canonical" href="' . get_permalink( $post->ID ) . '" />';
} else {
echo '<link rel="canonical" href="' . get_pagenum_link() . '" />';
}
}
// Add the function to the appropriate action hook
add_action( 'wp_head', 'cano_add_canonical_link' );
or
// create the canonical meta tag for the current page
function cano_add_canonical_link() {
global $post;
if( is_singular() ) {
$permalink = get_permalink( $post->ID );
} else {
$permalink = get_pagenum_link();
}
//rank math's function to insert the canonical link
if ( function_exists( 'rank_math' ) ) {
rank_math()->schema->set_data( 'canonical_url', $permalink );
if(!is_singular()){
//Get prev link
$prev_link = get_previous_posts_link();
//Get next link
$next_link = get_next_posts_link();
//If prev link exist set it as prev link rel canonical
if($prev_link) rank_math()->schema->set_data( 'prev_url', $prev_link );
//If next link exist set it as next link rel canonical
if($next_link) rank_math()->schema->set_data( 'next_url', $next_link );
}
}
}
// Add the function to the appropriate action hook
add_action( 'wp', 'cano_add_canonical_link', 20 );
Thank you
The ticket ‘Proper Canonical Tag help please’ is closed to new replies.