Last Updated: February 25, 2016
·
1.779K
· etienne_tremel

Get Menu By Location

Useful WordPress function to get a menu object from a given location.
Copy the following code into your functions.php

<?php
/**
 * Get Menu By Location
 * @param   string    $theme_location    Theme location
 * @return  mixed                        Menu Object or false if not found
 */
if ( ! function_exists( 'get_menu_by_location' ) ):
    function get_menu_by_location( $theme_location ) {
        $theme_locations = get_nav_menu_locations();
        $menu_obj = get_term( $theme_locations[ $theme_location ], 'nav_menu' );
        if ( $menu_obj )
            return wp_get_nav_menu_items( $menu_obj->term_id, $args );
        else
            return $menu_obj;
    }
endif;
?>