Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I set css class for menu items.

I want to get css class of current menu item and set it to body tag.

What should i do ?

share|improve this question
    
You can identify the current menu item by doing a .hasClass('current-menu-item') in jQuery... but what are you trying to do with it? I'm not clear. –  Imperative Ideas Oct 10 '13 at 7:32
    
Ex: I want change background base on menu items. So i need to add class of menu item to body. I do not want to use javascript, i want to do it with PHP –  Quang Trung Nguyen Oct 10 '13 at 7:53

2 Answers 2

You're going about this backwards. What you want to do is use jQuery to test for the presence of the current menu class, then see if that item has the assigned class. If it does, then you add your CSS to the body.

<script type="text/javascript">
    jQuery(document).ready(function($) {
        if($('li.current-menu-item').hasClass('your_custom_class')) {
            $('body').css('background-color', 'orange');
        }
    });
</script>

Integrating my comment... you probably want to cache the selector then loop through a few more times.

<script type="text/javascript">
    jQuery(document).ready(function($) {
        var menuitem = $('li.current-menu-item');
        if (menuitem.hasClass('your_custom_class')) {
            $('body').css({
                'background-color': 'orange',
                'background-image': 'url(http://someurl.com/thatimage.jpg'
            });
        }
        else if(menuitem.hasClass('your_custom_class')) {
            $('body').css({
                'background-color': 'orange',
                'background-image': 'url(http://someurl.com/thatimage.jpg'
            });
        }
    });
</script>
share|improve this answer
    
You should be able to add more CSS options using the .css({}) syntax, then loop through your other classes using elseif/elseif/elseif/else. You should probably cache $('li.current-menu-item') as well, since you'll be looping over it multiple times. –  Imperative Ideas Oct 10 '13 at 8:08
    
thank you, but i do not want to use javascript. I want to do it with PHP only –  Quang Trung Nguyen Oct 10 '13 at 8:12
    
Ok, have fun with that –  Imperative Ideas Oct 10 '13 at 8:18

'wp_get_nav_menu_items' by using this function you can get the menu items & associated pages/posts with it.

Below is the example

custom_menu_slug is the slug of menu.(theme location)

 $menu_name = 'custom_menu_slug';
    $menu = wp_get_nav_menu_object( 'custom_menu_slug' ); // get Menu ID from menu slug
    $menu_items = wp_get_nav_menu_items($menu->term_id); // $menu->term_id=Menu Id
    foreach ( (array) $menu_items as $key => $menu_item ) {
      if($menu_item->object_id==current_page_id){ // Your current page id 
      echo  $class = $menu_item->classes[0];
      }

    }
share|improve this answer

Your Answer

 
discard

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.