• Hello,

    I am building a plugin and need to apply certain jQuery stuff to the theme menu. If there are custom menus, the only hook for the menu items that I can find is the menu item ID (eg: menu-item-574) and I can’t seem to be able to get that ID using the $post->ID or some other data in order to use it with my jQuery code.

    Any advice is highly appreciated.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve been looking for a similar solution. Here’s some food for thought:

    The menu-item-ID corresponds to $wpdb->postmeta.post_id in the WordPress database.

    If you have a menu item which corresponds to a post_type="page" in $wpdb->posts, then you could look up the corresponding menu-item-ID for that page’s menu item by first finding the $wpdb->posts.ID for that page. Then you’d get the menu-item-ID for that page via the following query:

    $wpdb->get_var( 'SELECT post_id FROM '.$wpdb->postmeta.' WHERE meta_key="_menu_item_object_id" AND meta_value="'.$post_id.'"' )

    Here’s an example of the above in some code I’m currently working on:

    global $wpdb;
    $main_menu = array( 'Home', 'About', 'Blog', 'Resources', 'Contact' );
    foreach( $main_menu as $post_title ){
    	$post_id = $wpdb->get_var( 'SELECT ID FROM '.$wpdb->posts.' WHERE post_title="'.$post_title.'" AND post_type="page" AND post_status="publish"' );
    	$$post_title = $wpdb->get_var( 'SELECT post_id FROM '.$wpdb->postmeta.' WHERE meta_key="_menu_item_object_id" AND meta_value="'.$post_id.'"' );
    }

    The above code results in $Home, $About, etc. being assigned the menu-item-IDs for those pages. Therefore, I’m able to address those menu items via CSS selectors like these:

    li.menu-item-<?php echo $Home ?>{}
    li.menu-item-<?php echo $About ?>{}

    Notes:

    • I’m currently using the above method with a theme which uses only one instance of wp_nav_menu(). I’m guessing you would retrieve multiple menu-item-IDs if you had the same page in multiple occurrences of wp_nav_menu(). Therefore, you’d need to add some logic which would grab the proper menu-item-ID for the menu you want to address via jQuery, CSS, etc.
    • You could improve upon my above code by dynamically generating $main_menu.

    Oh dear!!!

    I’ve looked for this for so long, thanks a lot. I’ve even tried to understand how WordPress’ Walker class generates its menus ids, but I just got as far as seeing that each $item has a weird ‘db_id’ property — something which I couldn’t understand, all I got of it was that it, further down, translates (is used) as $element->ID, which is really confusing since one might think that is the same as $post->ID, while it isn’t! This is really confusing when trying to get the current item’s (page, post, etc) CSS class or id’s current-menu-id since it does not match the current item’s proper ->ID variable…

    Some of the things I searched for before finally getting here:
    menu id number
    menuid number
    menu-item id number
    menu-item id walker corresponding number
    menu-item walker corresponding number
    menu-item corresponding number
    menu item corresponding number
    menu item number
    display_element
    walk
    nav-menu
    nav-menu-template
    start_el
    current-menu-item
    current-menu-item-id
    current-menu-item-id-number

    Maybe these will help people searching on Google and whatnot.

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the menu item ID of a page if part of a custom menu’ is closed to new replies.