0

I need to add a "has-children" class to the tag if the current page's or post's menu item in fact has children.

As this "logic" has to work with Pages, Posts, Categories etc. menu items it seems it is not easy to find out.

Thanks in advance!

Edit: I am not sure I explained good enough: I need to add "has-children" not if the current page is a page-child but only if the current page's menu-item is a menu-item-child...

2 Answers 2

1

There is a body_class filter, if memory serves. That would be the right one to use.

2
  • As far as I know that doesn't help with how to find out if a page's menu-item is a parent of other menu-items, or does it?
    – Nick
    Commented Dec 12, 2010 at 9:29
  • Sorry. I assumed you'd know that you need to query the db using $wpdb to know that. Commented Dec 12, 2010 at 11:04
0

You could use some jQuery, i'd have thought a couple of lines of script is lighter then having to do lookup queries to figure out which items have children..

<script type="text/javascript">  
jQuery(document).ready( function($) {
    $('#my_menu_id li.current-menu-item').has('ul').addClass('has_children');
});
</script>

Of course making sure to update the selector to match your menu ID(s)/Class(es)...

Reference for jQuery has function. http://api.jquery.com/has/

1
  • That is probably easier than trying to run the check at the body_class filter, but I think he wanted to add the has_children tag to the body class, not to the menu item. Just a minor change. Commented Dec 12, 2010 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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