Show link to add menu

I’m using the following to show a menu

if ( has_nav_menu( 'topheader' ) ) {
    // User has assigned menu to this location;
    // output it
    wp_nav_menu( array(
        'theme_location' => 'topheader',
        'menu_class'    => 'topMenu', /* bootstrap ul */
        'walker' => new My_Walker_Nav_Menu(), /* changes to a bootstrap class */
        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
        'menu_id' => 'topMenu'
    ) );
}

If there are no links in the menu, nothing shows in the front-end. Is it possible to display a link to add a menu? Which takes the user to the menu location in dashboard.

2 Answers
2

Its possible:

    if (has_nav_menu('topheader')) {
        // User has assigned menu to this location;
        // output it
        wp_nav_menu(array(
            'theme_location' => 'topheader',
            'menu_class' => 'topMenu', /* bootstrap ul */
            'walker' => new My_Walker_Nav_Menu(), /* changes to a bootstrap class */
            'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
            'menu_id' => 'topMenu'
        ));
    }else{
        if (is_user_logged_in()) { //we check if the user is logged in
            echo '<a href="https://wordpress.stackexchange.com/wp-admin/nav-menus.php?action=edit&menu=0">Add New Menu</a>';
        }
    }

this will open the menu dashboard ready to create a new menu.

Leave a Comment