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.

Im trying to get the id of the menu item that has be class called "current-menu-item". Not the current page id, but the nav item id.

Please help

share|improve this question
    
I once figured out how to get the current menu item title, you should be able to adapt it to return the ID instead. –  Jan Fabry Jun 6 '11 at 22:04
add comment

3 Answers

This should solve it How to get current-menu-item title as variable?

but change

$GLOBALS['wpse16243_title'] = $menu_item->title;

to

$GLOBALS['wpse16243_title'] = $menu_item->ID;

and now you can use the global variable $wpse16243_title to get the ID

share|improve this answer
add comment

The best way would be to use the nav_menu_css_class filter. Something like this would work:

function wpse19375_nav_class( $classes, $item ){
  if( !in_array( 'current-menu-item', $classes ) )
    return $classes;
  // $item is a 'current-menu-item' object.
  return $classes;
}

add_filter( 'nav_menu_css_class', 'wpse19375_nav_class', 10, 2 );
share|improve this answer
    
thanks mate, but i don't understand how this will bring the id –  Leroy Jun 7 '11 at 9:08
    
The $item variable is the menu object. It has all the information you could want, including the ID. –  John P Bloch Jun 7 '11 at 10:55
    
@John P Bloch, But what are you returning $classes to? –  Steven Aug 15 '11 at 20:07
add comment

A little late perhaps, but there is one more way of doing it:

$menu = wp_get_nav_menu_items($menu_id,array(
   'posts_per_page' => -1,
   'meta_key' => '_menu_item_object_id',
   'meta_value' => $post->ID // the currently displayed post
));

var_dump($menu[0]->ID);

Since menu items are post-types you are able to use all the WP-Query params, even a meta query. The code above selects all menu_items which are connected to the current post, from the menu you specify via $menu_id.

share|improve this answer
add comment

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.