• Resolved Paulh267

    (@paulh267)


    I’ve got some woocommerce products on my website and I need to tell search engines that the source of content is actually on a different page for each of these products, so I’m using a canonical tag.

    This is the code I’m using:

    <?php
    if(is_product( array( '215-70-r16-h-achilles-desert-hawk-h-t', '225-55-r18-v-achilles-desert-hawk-h-t', '225-65-r17-h-achilles-desert-hawk-h-t', '235-50-r18-v-achilles-desert-hawk-h-t', '235-55-r18-h-achilles-desert-hawk-h-t', '235-60-r17-h-achilles-desert-hawk-h-t', '235-65-r17-h-achilles-desert-hawk-h-t', '245-65-r17-h-achilles-desert-hawk-h-t', '245-70-r16-h-achilles-desert-hawk-h-t', '255-50-r19-h-achilles-desert-hawk-h-t', '255-55-r18-h-achilles-desert-hawk-h-t', '255-70-r16-h-achilles-desert-hawk-h-t', '265-65-r17-h-achilles-desert-hawk-h-t', '275-70-r16-h-achilles-desert-hawk-h-t')))
    {
    echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/achilles-tyres/desert-hawk-h-t/" />';
    }
    ?>

    For some reason it’s outputting this at the top of the page:
    '; } ?>

    And when I go to the page source it says:
    <link rel="canonical" href="https://tyresdirectuk.co.uk/shop/tyres/235-50-r18-v-achilles-desert-hawk-h-t/" />

    If anyone could advise on this situation it would be appreciated 🙂

    Link: https://tyresdirectuk.co.uk/shop/tyres/235-50-r18-v-achilles-desert-hawk-h-t/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi there,

    I do not see the '; } ?> being output on top of the page as you are describing. Perhaps that problem stopped or you have already fixed it on your own?

    However the canonical url is indeed as you say. First of all it is important to ask where you have put your code above. Is it exactly where the canonical url is echoed in the head.php or is it used inside a function that runs inside a hook?

    Thread Starter Paulh267

    (@paulh267)

    Hi Nevma,

    I played around with the code some more and managed to get it to work (kind of).

    This an example of the code we have currently. All code is in our header.php file:

    <?php
        // Achilles Desert Hawk UHP
        if(is_product( array( '245-40-r20-99v-achilles-desert-hawk-uhp-e-c-72', '245-45-r20-99v-achilles-desert-hawk-uhp-e-c-72', '255-45-r20-105v-achilles-desert-hawk-uhp-f-c-72' )))
        {
        echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/achilles-tyres/desert-hawk-uhp/" />';
        }
    
        // General Grabber GT
        elseif(is_product( array( '195-80-r15-96h-general-grabber-gt-e-c-71', '205-70-r15-96h-general-grabber-gt-e-c-71', '205-80-r16-104t-general-grabber-gt-e-c-72', '215-60-r17-96h-general-grabber-gt-e-c-71', '215-65-r16-98h-general-grabber-gt-e-c-71', '215-65-r16-98v-general-grabber-gt-e-c-71', '215-70-r16-100h-general-grabber-gt-e-c-71', '225-55-r17-97v-general-grabber-gt-e-c-71', '225-55-r18-98v-general-grabber-gt-e-c-71', '225-60-r18-100h-general-grabber-gt-e-c-71', '225-65-r17-102v-general-grabber-gt-e-c-71', '225-70-r16-103h-general-grabber-gt-e-c-71', '235-50-r18-97v-general-grabber-gt-e-c-71' )))
        {
        echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/general-tyres/grabber-gt/" />';
        }
    
    // Kumho Road Venture AT KL78
    elseif(is_product( array( '30-950-r15-104s-kumho-road-venture-at-kl78-f-e-78', '195-80-r15-100s-kumho-road-venture-at-kl78-e-e-74', '205-75-r15-97s-kumho-road-venture-at-kl78-e-e-74', '205-80-r16-104s-kumho-road-venture-at-kl78-e-e-74')))
    {
    echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/kumho-tyres/road-venture-at-kl78/" />';
    }
    ?>

    The problem is that the respective canonical links aren’t being outputted to the correct pages.

    Here’s an example: https://tyresdirectuk.co.uk/shop/tyres/205-75-r15-97s-kumho-road-venture-at-kl78-e-e-74/

    This should have the following line of code between the header tags:
    <link rel="canonical" href="https://tyresdirectuk.co.uk/kumho-tyres/road-venture-at-kl78/" />

    but instead it says:
    <link rel="canonical" href="https://tyresdirectuk.co.uk/achilles-tyres/desert-hawk-uhp/" />

    If anyone could please advise on this it would be much appreciated as I can’t figure it out 🙂

    Seems like the is_product WooCommerce function is not working properly. Are you sure it expects the argument you have given to it? I would suggest trying to narrow the problem down by checking if the is_product() works with only one product inside it. And first check with the product ID (that is the post ID). Does the function work then?

    Thread Starter Paulh267

    (@paulh267)

    I think you might be right about the is_product(). I just tried this:

    <?php
    // Achilles Desert Hawk UHP
    if(is_product( array( 28596, 28595 )))
    {
    echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/achilles-tyres/desert-hawk-uhp/" />';
    }
    
    // General Grabber GT
    elseif(is_product( array( 11170, 11171 )))
    {
    echo '<link rel="canonical" href="https://tyresdirectuk.co.uk/general-tyres/grabber-gt/" />';
    }
    ?>

    Product ID 11170 is still displaying the following in it’s source code between the header tags:
    <link rel="canonical" href="https://tyresdirectuk.co.uk/achilles-tyres/desert-hawk-uhp/" />

    I’ll get in contact with both the theme developer and woothemes to check if there are any known problems on their side.

    Thanks for the help 🙂

    Welcome! 🙂

    Thread Starter Paulh267

    (@paulh267)

    For anyone coming here having the same problem. The problem was the is_product() code. It needs to be is_single()

    Problem solved! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Canonical tag problem’ is closed to new replies.