WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wonder if someone has an answer to this or could point me in the right direction

I have a woocommerce product which resides in multiple categories like so

            /**the product categories structure is something like this**/
             (id 1)  cat-a
             (id 2)     -subcat-a-1
             (id 3)         -subcat-a-1-1
             (id 4)         -subcat-a-1-2 (product in this category)
             (id 5)         -subcat-a-1-3
             (id 6)     -subcat-a-2
             (id 7)         -subcat-a-2-1
             (id 8)         -subcat-a-2-2
             (id 9)         -subcat-a-2-3
             (id 10)        -subcat-a-3
             (id 11) cat-b
             (id 12)    -subcat-b-1
             (id 13)    -subcat-b-2
             (id 14)    -subcat-b-3
             (id 15)        -subcat-b-3-1 (same product in this category too)
             (id 16)        -subcat-b-3-2
             (id 17)        -subcat-b-3-3   

             (id 18) cat-c
            .......and so forth......

i also have the woocommerce navigation menu on the product category as well as single product page. when i go to a category like


the right products gets shown and the navigation selects/shows the right subcategory. so far so good.

however, when then going to a product the naviagtion on the single product page always selects the same category , regardless of where we came from so to speak. I realise that the links to products on category pages do not include the category we were coming from, so I i added the following to "archive-product.php"

            /**get currently selected category, so we can 
                add cat id to product link to select appropriate 
                side nav cat when selected
            global $wp_query,$msci_cat_obj;
            $msci_cat_obj = $wp_query->get_queried_object();

and this to "content-product.php"

            global $msci_cat_obj;
                some more stuff

            /*changing link to add category id to url**/
            <a href="<?php the_permalink(); ?><?php echo $msci_cat_obj->term_id; ?>" title="<?php the_title() ?>">
                even more stuff

which now gives me a url to the product like so


respectively depending on which (sub)category I am in.

obviously , when now going to the respective product page by using aforementioned links i can explode (or whatever) the url to just get the category id I came from.

What I do not know (and have tried from days to figure out ) is how to use this id to persuade the navigation to set this category to be selected in the navigation on the single product page and not always the same one.

Any hints/suggestions/links much appreciated.

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.