tags:

views:

79

answers:

2

Hi,

I have the following menu setup that basically has a parent menu of "Products" with two child menu items that I am using inside my WordPress 3 menu structure, specifically inside my sidebar.php file:

<ul id="themenu" class="sf-menu sf-vertical">
        <li><a class="sf-with-ul topm" href="#">Products</a>
            <li><a href="information">Information</a></li>
            <li><a href="parts">Parts</a></li>
        </li>
</ul>

What I am unsure how to do using PHP is when a user clicks on either of the child menu options, i.e."Information or Parts", I would like to add a css class of currentMenu to it's parent menu class, i.e.:

<li><a class="sf-with-ul topm currentMenu" href="#">Products</a>

Would appreciate your help on this.

Thanks.

A: 

How about using jQuery instead like this:

$('#themenu a').click(function(){
  $(this).closest('.topm').addClass('currentMenu');
  return false;
});
Sarfraz
Sarfraz - can I add this within my sidebar.php file - if so, where?
tonsils
+2  A: 

Wordpress does this for you. It adds classes to your menu's (assuming you're using wp_list_pages).

The classes are current_page_parent and current_page_ancestor

EDIT: If you're printing the nav statically you can do this

<ul id="themenu" class="sf-menu sf-vertical">
        <li><a class="<?php if( in_array( $_SERVER['REQUEST_URI'], array( '/information','/parts' )  ) ?>currentMenu<?php endif; ?>" href="#">Products</a>
            <li><a href="information">Information</a></li>
            <li><a href="parts">Parts</a></li>
        </li>
</ul>

It checks the url and if the url is in the list it will set the class to currentMenu. You have to edit the array of urls for that parent to suit your needs.

Galen
Actually am not using wp_list_pages. Can I still use these two classes that you have mentioned?
tonsils
not if you're printing the nav statically no
Galen
Hi @Galen. With your answer, locally my url is: http://localhost:8888/abc when first entering my site. So with your array( '/information','/parts' ), this doesn't seem to work as the in_array is actually looking for '/abc/information' and '/abc/parts' - any ideas?Secondly, when I finally go live with my site, i.e. http://www.abc.com, will your answer cover this scenario or will I need to also add /abc/information' and '/abc/parts' as part of the array part? Thanks.
tonsils
you have to edit the array so that it fits your site
Galen
No worries Galen - gotcha. Thanks.
tonsils