Is possible to get current-menu-item as a php variable?

I'm listing the category museums via the menu item 'museums we support' so i'd like the get 'museums we support' bit and display it somewhere?

Any help appreciated!

4 Answers 4


This is possible by filtering wp_nav_menu_objects, which is the easiest place to check which item is the current menu item, because WordPress already added the classes for you.

add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
    foreach ( $sorted_menu_items as $menu_item ) {
        if ( $menu_item->current ) {
            $GLOBALS['wpse16243_title'] = $menu_item->title;
    return $sorted_menu_items;

You can now use this new global variable instead of the normal title. Simple example:

add_filter( 'single_cat_title', 'wpse16243_single_cat_title' );
function wpse16243_single_cat_title( $cat_title )
    if ( isset( $GLOBALS['wpse16243_title'] ) ) {
        return $GLOBALS['wpse16243_title'];
    return $cat_title;

Of course, this only works if you display the menu before you display the title. If you need it earlier (maybe in the <title> element?), you should first render the menu and then display it later.

  • hello Jan - just wondering if there's a way to create an array of the $menu_item->current items' children? Tried several things but not getting very far..!
    – v3nt
    Commented Jul 19, 2011 at 11:00
  • managed this with the help of your filter - answer below. Cheers!
    – v3nt
    Commented Jul 19, 2011 at 12:59
  • Jan Fabry / I tried using wpse16243_wp_nav_menu_objects to retrieve current menu ID. But it's not consistant. Switching between home page and a menu item does nto always give me correct menu item - it kind of "lags". Any suggestion on how I can retrieve current menu ID?
    – Steven
    Commented Aug 15, 2011 at 20:00
  • Is there a reason why you preface the function name with wpse16243? I see this strange type of name fairly common and wondering what it's all about. Disclaimer: Wordpress noob. Commented Jan 28, 2012 at 15:32
  • 2
    @Steve: Just a unique prefix: WordPress Stack Exchange question number 16243. If people directly copy this code, it won't conflict with other plugins that might share the same function names.
    – Jan Fabry
    Commented Jan 29, 2012 at 10:47

You can use wp_get_nav_menu_items(). Here's a sample function:

function my_get_menu_item_name( $loc ) {
    global $post;

    $locs = get_nav_menu_locations();

    $menu = wp_get_nav_menu_object( $locs[$loc] );

    if($menu) {

        $items = wp_get_nav_menu_items($menu->term_id);

        foreach ($items as $k => $v) {
            // Check if this menu item links to the current page
            if ($items[$k]->object_id == $post->ID) {
                $name = $items[$k]->title;

    return $name;

Then call the function with the location name of the nav menu you wish to use:

$menu_name = my_get_menu_item_name( 'NAV_MENU_LOCATION_NAME' ); // eg. 'primary'

echo $menu_name;
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
    foreach ( $sorted_menu_items as $menu_item ) {

        if ($menu_item->current ) {
            $GLOBALS['currentMenuTitle'] = $menu_item->title;
            $GLOBALS['currentMenuID'] = $menu_item->ID;


    return $sorted_menu_items;

function get_menu_items_children( $menu_item_id='' )
    global $wpdb;

      //     AND meta_key='_menu_item_menu_item_parent'

    $myrows = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta JOIN $wpdb->posts ON $wpdb->postmeta.post_id=$wpdb->posts.id WHERE meta_value = '".$menu_item_id."' AND meta_key='_menu_item_menu_item_parent' ORDER BY $wpdb->posts.menu_order ASC" );

    foreach ( $myrows as $menu_item ) {

        $sc = get_object_vars($menu_item);  

        $myrowsb = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE post_id = '".$sc['post_id']."' AND meta_key='_menu_item_object_id' " );

        foreach ( $myrowsb as $menu_itemb ) {

            $scb = get_object_vars($menu_itemb);    
            $pmIDs[] = $scb['meta_value'];



    return $pmIDs;

I'm not sure I exactly follow.

The "Museums We Support" is generated by a Post Title, Page Title, Category Title, etc. (or, if you're using a Custom Navigation Menu, it could be a custom Title).

What are you trying to do with this text? Where are you trying to display it? I assume you're trying to display it on the Category Index Page (since you indicate it is current-menu-item)? If so, simply call <?php single_cat_title(); ?> to output the Category Title.

If you need additional category information, you can use something like:

$cat = get_the_category();
$currentcat = $cat[0];

Which makes available the following variables:

$currentcat=>cat_ID // Category ID
$currentcat=>cat_name // Category Name (Same as Title)
$currentcat=>category_nicename // Category Slug
$currentcat=>category_description // Category Description
$currentcat=>category_count // Category Count (# of Posts w/ this Category)

If you're somewhere other than the Category Index Page, you'll need to pass the $catid to these functions.


Since you indicated that "Museums We Support" is a custom Menu Title, the easiest approach would be to rename the Category from "Museums" to "Museums We Support" (note: you can leave the slug, museums, unchanged). However, doing so would mean that "Museums We Support" would be displayed as the Category Title wherever else it might be output in your template. If that is acceptable, then no worries.

Otherwise, you will have to use something like wp_get_nav_menu_items() (Codex ref) in order to grab the Title for the specific menu item.

  • hey chip - its this page ; bfami.modernactivity.co.uk/category/museum so basically the cat is 'museum' but i want to show the 'MUSEUMS WE SUPPORT' which is the navigation label (custom title) set in the menus page. Think its possible to grab that somehow?
    – v3nt
    Commented May 3, 2011 at 15:44
  • That helps. See my answer edit. You have a couple options: rename the Category, or use wp_get_nav_menu_items(). Commented May 3, 2011 at 16:11
  • cheers chip - just changed the category title in the end! Just thought there would be an easy way to grab the menu label...
    – v3nt
    Commented May 3, 2011 at 17:02

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.