Support » Developing with WordPress » Theme preview doesn’t display my menu

  • DediData

    (@dedidata)



    Hi,

    I developed a theme and it has already these menus:

    register_nav_menus( array(
    	'primary'			=> esc_html__( 'Top Menu', 'free-template' ),
    	'header'			=> esc_html__( 'Bottom of Header', 'free-template' ),
    	'header-right'	=> esc_html__( 'Bottom of Header - Right', 'free-template' ),
    	'bottom'		=> esc_html__( 'Bottom of Site', 'free-template' ),
    ) );

    But theme preview doesn’t display any menu in preview mode
    What should I do ?
    Thank you for any help

    • This topic was modified 3 months ago by  DediData.

    The page I need help with: [log in to see the link]

Viewing 13 replies - 1 through 13 (of 13 total)
  • Radu Constantin

    (@raduconstantin)

    Hi,

    Have you set any menu for the locations you registered? If yes, have you used wp_nav_menu to display the menu on your website? Sharing the code of the theme you created would be very helpful in order to see what’s going on here.

    DediData

    (@dedidata)

    @raduconstantin

    I mentioned for “Preview of theme”

    Moderator bcworkz

    (@bcworkz)

    Does the version in the repository display this behavior? Can we look at that code, or are you working on a dev version that’s different?

    Joy

    (@joyously)

    The theme previewer does not have a menu chosen for any location because menu locations can be called any name. Your theme probably should have a menu fallback for when no menu is chosen. It is the default value of the menu function, so you had to override it to get nothing.

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Greetings!

    Part of the reason is because you are checking for the existence of the primary menu. You’re checking that here: https://themes.trac.wordpress.org/browser/free-template/4.3.5/template-parts/part/header-top-nav.php#L2

    Because the primary menu is not assigned on the site that uses the preview data it skips over that and no menu is displayed.

    DediData

    (@dedidata)

    Hi,
    @bcworkz, the version on repo is ok for this test

    @joyously, I don’t think so, other themes don’t do that, I think there is a menu in that preview site

    @jcastaneda, Yes, But it should not make any problem, the preview site probably have a menu, so if that menu is in primary location, so it should work

    Friends, I mean the preview on WP.org site, not theme preview on our own site

    • This reply was modified 3 months ago by  DediData.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    the preview site probably have a menu

    And all cars probably have tinted windows because all cars have windows, right? Think about what you just said for a moment too.

    Friends, I mean the preview on WP.org site, not theme preview on our own site

    Yes, we understand that.

    Let me break this down a little bit for you and hopefully it makes sense for you. Let’s take a look at how wp_nav_menu() works. as well as has_nav_menu()

    First, wp_nave_menu(). In the developer docs ( https://developer.wordpress.org/reference/functions/wp_nav_menu/ ) you can see that it does quite a bit in about 200 lines of code. A lot of that being comment code. The $defaults is somewhat important here because it is a fallback ( which Joy mentioned ) that WordPress will use if something is not passed. The first thing it tries is getting a menu object by the passed name. Done with:

    
    wp_get_nav_menu_object( $args->menu );
    

    WordPress then tries to get the menu items based on the theme location in the next few lines. Now, this part:

    
        /*
         * If no menu was found:
         *  - Fall back (if one was specified), or bail.
         *
         * If no menu items were found:
         *  - Fall back, but only if no theme location was specified.
         *  - Otherwise, bail.
         */
    

    It may seem a little odd to grok at first but it will make sense eventually. About this point in the code is when the $defaults comes into play because the fallback_cb is a default of wp_page_menu, which is a core function.

    Great! We know we can show a menu with a fallback.

    But what about the function has_nav_menu() you’re asking, right? What role does that have in all of this?

    Everything!

    Let’s see how that one works, yeah? In the developer docs: https://developer.wordpress.org/reference/functions/has_nav_menu/

    Wow, in about less than 20 lines of code. I’m going to quote the documentation because this is the key part:

    Determines whether a registered nav menu location has a menu assigned to it.

    I emphasized the key part. That is something a user assigns not the theme. I’m sure it can be done by a theme/plugin but that would sort of defeat the purpose of giving a user choice. I will also point you to Twenty Sixteen and Twenty Fifteen since, they too, use the function has_nav_menu() and don’t display a menu. This was a design choice if memory serves me right.

    I guess really the main answer would be if you want to show that menu on the preview, simply remove the has_nav_menu() check and you’ll be fine.

    DediData

    (@dedidata)

    @jcastaneda, Thank you for your explain and you time to help other members
    I can’t remove has_nav_menu() because of the smart displaying menu bar,
    But can I detect if we are in preview mode of WP.org?
    So I can add a logic phrase to the if statement to check that and let the menubar display
    I mean this function:
    https://codex.wordpress.org/Function_Reference/is_customize_preview
    Does it work on WP.org ?
    Thank you man

    • This reply was modified 3 months ago by  DediData.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    But can I detect if we are in preview mode of WP.org?

    No. You do that and you get the theme suspended.

    DediData

    (@dedidata)

    @jcastaneda
    Why?
    using is_customize_preview is prohibited?

    Joy

    (@joyously)

    Using is_customize_preview is not prohibited, but coding your theme specifically for the previewer is. Besides, that function wouldn’t do what you want. Themes should not try to select a menu because they can be called anything. Using the core fallback is the best way.

    @joyously, @jcastaneda
    I don’t like to trick a thing which my theme doesn’t support
    My theme supports 4 menu positions, and if those positions has any menu assigned, So their container will be display in there and it display menu too
    But in WordPress.org preview, none of them display,
    I put is_customize_preview in my code too, But it seems it doesn’t work too in WP.org preview
    What should I do ?
    I need to show the users those menus in preview

    Use a fallback function to define a menu for times when an existing menu is not defined by the user. This will be what is shown on the .org preview. Remember that the menus defined on .org previewer are unlikely to match your menu name exactly so that is privacy why this is not working like you expect.

    Please do not try to directly target the customizer/previewer from .org in your code as that is against theme review team guidelines.

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.