Home Custom Menu Link not Working

I’m trying to have the menu link per default when using my custom menus, and in my functions.php document I have this:

function home_page_menu_args( $args ) {
    $args['show_home'] = true;
    return $args;
}
add_filter( 'wp_page_menu_args', 'home_page_menu_args' );

The thing is, in the custom menu options > pages I have this: “No items.” So not sure what’s going on, it was working in other websites but not in this one, any idea?

Related posts

1 comment

  1. First, I’ll assume that you have custom nav menus properly configured:

    1. register_nav_menus() in functions.php, to define theme_location values
    2. wp_nav_menu() calls in the template, with theme_location called in the args array
    3. Custom nav menus defined in the admin
    4. Custom nav menu(s) assigned to Theme Locations

    If that’s the case, then the issue is that you’re using the wrong filter. The wp_page_menu_args filter is applied inside of wp_page_menu(), which is the default callback for wp_nav_menu() when no menu is assigned to the indicated theme_location.

    The output of wp_nav_menu() applies its own filter: wp_nav_menu_args. So you’ll need to hook your callback into that filter as well:

    function home_page_menu_args( $args ) {
        $args['show_home'] = true;
        return $args;
    }
    add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
    // Hook into wp_nav_menu
    add_filter( 'wp_nav_menu_args', 'home_page_menu_args' );
    

    That way, the show_home arg will return true for both wp_page_menu() output and for wp_nav_menu() output.

    Be careful with wp_nav_menu(), though; if the user adds a home link to the custom menu, then two home page links will be displayed in the rendered menu.

Comments are closed.