Setting CSS class of parent menu from child menu using PHP / WordPress

I have the following menu setting, which basically has a parent “Products” menu with two child menu items that I use inside my WordPress 3 menu structure, in particular, 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>

I'm not sure what to do with PHP when the user clicks on any of the parameters of the child menu, that is, "Information or parts", I would like to add the CSS class currentMenu to its parent menu class, that is:

<li><a class="sf-with-ul topm currentMenu" href="#">Products</a>
+3
source share
2 answers

WordPress . ( , wp_list_pages).

current_page_parent current_page_ancestor

,

<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>

URL- , URL- , currentMenu. URL .

+2

jQuery :

$('#themenu a').click(function(){
  $(this).closest('.topm').addClass('currentMenu');
  return false;
});
0

Source: https://habr.com/ru/post/1760907/


All Articles