0

I have created a tag.php page in the theme. It's working perfectly. I am getting the tags post on the page.

Now I have added the below link to the header.php page.

<link rel="alternate" hreflang="en-US" href="<?php echo wp_get_canonical_url(); ?>"/>

The above code is showing the canonical URL perfectly on all the pages but when I open my tag page then it's not showing the canonical URL. It's showing the first URL of the post

Any idea what is the issue?

tag.php code below

<?php
   /**
   * A Simple tag Template
   */
   get_header(); 

?>  
<div id="primary" class="content-area">
   <main id="main" class="site-main">

    <section class="resource-container" id="light-header">


        <div class="container">
            <?php $tag = get_queried_object();?>
            <h1><?php echo $tag->name; ?></h1>
       </div> 
          
 </section> 

   </main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 Answer 1

4

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:

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.

6
  • My aim is to show only the current URL. So I tried wp_get_canonical_url. Thank you for sharing the information, Let me check the above links Jul 23, 2022 at 9:34
  • if that's the case then you shouldn't be using alternate, the alternate is for an alternative different URL that is equivalent but in a different language. If the goal was to indicate to google that the page was in en_US then this is not the solution. This tag is for when you're on an english page but want to tell Google "this is also available here in spanish"
    – Tom J Nowell
    Jul 23, 2022 at 12:16
  • Little bit confused here. I have to show that the page is in en_US and it's ready in English. My target audience is from the USA. That's the reason I am using that hreflang="en-US". Jul 23, 2022 at 15:07
  • That link was shared by the SEO team and said I have to add it along with the page URL on each page. So I thought wp_get_canonical_url() is the right to show the current URL of the page. Any ideas and suggestions for this? Jul 23, 2022 at 15:09
  • wp_get_canonical_url is only for single posts, not archives.
    – Tom J Nowell
    Jul 23, 2022 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.