• gilgimech

    (@gilgimech)


    What I’m trying to do is to create a variable that outputs the nav menu ID by the nav menu name or slug. Like the way you can get a category id.
    I know you can do it with a database query, but I don’t know queries to well.
    Somethine like this.

    global $wpdb;
    $menu_slug = 'top-menu';
    $menu_id = $wpdb->get_results(
    	"
    	SELECT TERM_ID
    	FROM $wpdb->wp_terms
    	WHERE name = ".$menu_slug."
    	"
    );
    echo $menu_id;

    So what I want to do is find the name or slug of the nav menu top-menu and get the ID.

Viewing 1 replies (of 1 total)
  • The solution is clear if you look at wp_nav_menu() code. Here’s what you needed:

    $menu_slug = 'top-menu';
    $locations = get_nav_menu_locations();
    
    if (isset($locations[$args->theme_location])) {
        $menu_id = $locations[$args->theme_location];
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to Get Nav Menu ID’ is closed to new replies.