This is because wp_get_canonical_url
can only be used on posts, and cannot be used for tags authors archives etc.
Returns the canonical URL for a post.
https://developer.wordpress.org/reference/functions/wp_get_canonical_url/
More specifically, no equivalent exists for tag archives because archives and listings do not have canonical URLs. A canonical URL indicates to a search engine the correct and canonical location of a piece of content, but an archive is not an item of content, it’s a collection/list.
Instead, it would be more logical to use the archive URL of the queried term, say get_term_link
:
https://developer.wordpress.org/reference/functions/get_term_link/
Just keep in mind that this too is semantically incorrect, the alternate
link rel is for specifying alternative countries and locales, not the canonical URL:
- https://developers.google.com/search/blog/2011/12/new-markup-for-multilingual-content
- https://developers.google.com/search/docs/advanced/crawling/localized-versions?hl=en&visit_id=637940968921291027-2794865761&rd=1
If you’re using a multilingual plugin then you will need to look up how they handle this or your tag will always refer to the current locale.