WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Issue with WP Menu function (4 posts)

  1. sc456a
    Member
    Posted 1 year ago #

    I'm working on a theme and I've added the following code to functions.php:

    add_filter( 'get_search_form', 'my_search_form' );
    function register_my_menus() {
      register_nav_menus(
        array( 'header-menu' => __( 'Header Menu' ) )
      );
    }
    add_action( 'init', 'register_my_menus' );

    I've added the following to header.php:

    <h3><?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?></h3>

    This properly displays the menu if the menu is setup and selected in Appearance>Menus. The problem is that if no menu is setup, and therefore no menu is selected, then WordPress simply displays whatever pages are setup on the site. How can I get WordPress to only display the menu if one is setup and not list pages? Thanks!

  2. sc456a
    Member
    Posted 1 year ago #

    Ignore that add_filter line. Ooops.

  3. sc456a
    Member
    Posted 1 year ago #

    Just figured part of it out. Since I'm not using language translation, I changed:

    function register_my_menus() {
      register_nav_menus(
        array( 'header-menu' => __( 'Header Menu' ) )
      );
    }
    add_action( 'init', 'register_my_menus' );

    to:

    function register_my_menus() {
      register_nav_menus(
        array( 'header-menu' => 'Header Menu' )
      );
    }
    add_action( 'init', 'register_my_menus' );

    That got rid of an error in the Theme Checker, but it's still showing pages when the menu isn't active...

  4. sc456a
    Member
    Posted 1 year ago #

    Fixed it! Added fallback_cb to the header code:

    <h3><?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'fallback_cb' => '' ) ); ?></h3>

Topic Closed

This topic has been closed to new replies.

About this Topic