Outputs rel=canonical for singular queries.
function rel_canonical() {
if ( ! is_singular() ) {
return;
}
$id = get_queried_object_id();
if ( 0 === $id ) {
return;
}
$url = wp_get_canonical_url( $id );
if ( ! empty( $url ) ) {
echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
}
}
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rel_canonical