I am trying to use a menu which I have partially working, I have setup functions.php:
if (function_exists('register_nav_menu')) {
register_nav_menu('main-menu', 'Main Menu');
}
I have configured the menu in Appearance -> Menus with 2 Pages: Home & Sample Page.
My Problem:
When I call the menu using wp_nav_menu() like this:
CODE#1
wp_nav_menu( array( 'theme_location' => 'main-menu', 'container' => false ) );
I get no menu - nothing prints to screen
When I access the elements manually like this:
CODE#2
$menu_name = 'main-menu';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<ul id="menu-' . $menu_name . '">';
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
}
$menu_list .= '</ul>';
} else {
$menu_list = '<ul><li>No Menu</li></ul>';
}
echo $menu_list;