28 Dec 2016

Test Eng

I’m currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.

I’ve been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)

Now, I’m trying to figure out how to add a class of “active” to the currently selected product and can’t seem to figure it out?

In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class=”active” to the li?

I’ve made numerous searches and haven’t been able to come up with anything useful, so I’m turning here.

Any help greatly appreciated!!

shareimprove this question

3 Answers

If the query hasn’t been modified by a plugin for some reason, you should be able to get a single product page’s “id” via

global $post;
$id = $post->id

OR

global $product;
$id = $product->id;
shareimprove this answer
I am not able to retrieve the current product id using any of the above two methods. I am using custom plugin to access the current plugin displayed. Please could you help me… – therealprashant Oct 16 ’15 at 7:00
Inside the loop this works. If you are trying to access the variable on an early hook (such as init) then it won’t work. I can’t help you further here. You need to ask your own specific and detailed question. – helgatheviking Oct 16 ’15 at 13:54
Hey thanks for the comment .Sadly I got no question asking power anymore on SO. Could you direct me to some other stream where you could help me. Thanks a lot – therealprashant Oct 19 ’15 at 6:35

Save the current product id before entering your loop:

$current_product = $product->id;

Then in your loop for your sidebar, use $product->id again to compare:

 <li><a <? if ($product->id == $current_product) { echo "class='on'"; }?> href="<?=get_permalink();?>"><?=the_title();?></a></li>
shareimprove this answer

Retrieve the ID of the current item in the WordPress Loop.

echo get_the_ID(); 

hence works for the product id too. #tested #woo-commerce

shareimprove this answer

Your Answer

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

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

Goto Top