WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_nav_menu() fallback not receiving vars (6 posts)

  1. Edward McIntyre
    Member
    Posted 10 months ago #

    I developed a custom walker class to implement the Bootstrap 3.0 menu structure so menus can be edited using the WordPress menu editor.

    https://github.com/twittem/wp-bootstrap-navwalker

    Everything works great except for the $fallback_cb which is used by default when the theme is installed. I read in the codex that

    $fallback_cb
    (string) (optional) If the menu doesn't exist, the fallback function to use. Set to false for no fallback. Note: Passes $args to the custom function.

    The issue seems to be that the wp_page_menu function is not receiving the $args from the wp_nav_menu() declaration.

    Here is my current wp_nav_menu() declaration.

    <?php
    wp_nav_menu( array(
          'menu'           => 'primary',
          'theme_location' => 'primary',
          'depth'          => 2,
          'container'      => false,
          'menu_class'     => 'nav navbar-nav',
          'fallback_cb'    => 'wp_page_menu',
          'walker'         => new wp_bootstrap_navwalker())
    );
    ?>

    Can anyone offer some insight into how I might resolve this issue?

  2. esmi
    Forum Moderator
    Posted 10 months ago #

    The idea with the fallback_cb is that you set up your own fallback function - which may (or may not) make use of wp_page_menu().

  3. Edward McIntyre
    Member
    Posted 10 months ago #

    I definitely understand that, is guess the question is fallback_cb is undefined and defaults to wp_page_menu is there any way to pass the defined wp_nav_menu vars over to wp_page_menu?

  4. esmi
    Forum Moderator
    Posted 10 months ago #

    From what I recall, wp_nav_menu() uses slightly different parameters to wp_page_menu(). So I'd guess that there no easy way of simply passing them from one function to the other.

  5. bcworkz
    Member
    Posted 10 months ago #

    It is true that wp_page_menu() uses different parameters, but any arguments you supply to wp_nav_menu() will be passed to the 'fallback_cb' function. Whether your 'fallback_cb' function or wp_page_menu() can make use of these arguments is another story.

    The key line in wp_nav_menu() source code is:
    call_user_func( $args->fallback_cb, (array) $args );

    where $args is the merge of passed and default arguments, so you can see all arguments are passed to the 'fallback_cb' function. If this does not appear to be happening, it could be the wp_page_menu() does not know how to use a particular argument, but the argument is being passed none the less.

  6. Edward McIntyre
    Member
    Posted 10 months ago #

    Interesting. Looks like the $args are being passed to my wp_page_menu() function. After some further digging I notice that does not accept the container & walker parameters. Time to write my own.

    Thanks for sharing your knowledge!

Reply

You must log in to post.

About this Topic