0

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;

2 Answers 2

1

I'm not sure if I've followed you correctly; but I think you're trying to build the Menu HTML entirely on your own. If I'm correct, then what you're trying to do is covered under "Walker" object of wp-nav-menu.

You need to write a separate function in your functions.php and then pass the function name as "walker" to wp_nav_menu. It's not as easy as letting Wordpress do the HTML of Menu for you, but not too difficult as well.

I suggest you go through following link to get more idea of "Walker" stuff: Menu items description? Custom Walker for wp_nav_menu().

Let us know if this solves your problem.


*P.S.: May I know why you want to build HTML of Menu manually when Wordpress can do it for you? I understand, sometimes theme's artistic illustration of menu-bar makes it difficult for us to find that required design-redundancy to pass to wp_nav_menu... but if this not the case, then I feel you've opted a hard way to achieve desired results.

4
  • Hi, I'm not trying to build the menu myself - I simply did so because I could not get wp_nav_menu() to work (and it still doesn't) My problem is that wp_nav_menu() is not printing my menu (main-menu) ... and to prove that main-menu exists, I used the wp_get_nav_menu_items() procedure described at codex.wordpress.org/Function_Reference/… which worked... So my question is - why won't wp_nav_menu() show my menu? (which is just 2 Pages created in Wordpress (Home & Sample Page) Commented Feb 26, 2012 at 21:25
  • see screenshot: dl.dropbox.com/u/5456769/appearance-menu.png Commented Feb 26, 2012 at 21:28
  • I had a look at your screenshot and everything looks good there. Can you please copy-paste the code-section from your header.php where you're actually defining and using wp_nav_menu? This should help me better. Have you compared your code with any of the Wordpress default themes, like Twenty Ten? I think we all will be able to get you going better and faster if we get to see your code.
    – Ruturaj
    Commented Feb 27, 2012 at 9:58
  • Hi - all the code I used is in the OP - I have shown both configurations of code that I have used - only the 2nd piece of code displays a menu Commented Feb 27, 2012 at 21:20
0

Make sure you're adding the register_nav_menu function inside of after_theme_setup. Here is an example:

<?php
  add_action( 'after_setup_theme', 'your_newtheme_setup' );
  if ( ! function_exists( 'your_newtheme_setup' ) ) :
  function your_newtheme_setup() {
    if (function_exists('register_nav_menu')) {
      register_nav_menu('main-menu', 'Main Menu');
      }
    add_theme_support('post-thumbnails');    
    // Add other Theme Suppotrs inside the after_theme_setup function
    //...
    //...
    }
  endif;
?>
1
  • Did that but it made no difference. I was already using register_nav_menu() just not using 'after_theme_setup' Commented Feb 26, 2012 at 8:14

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.