WordPress.org

Ready to get started?Download WordPress

Forums

wp_nav_menu() and wp_page_menu() generate completely different markup (12 posts)

  1. Dunhamzzz
    Member
    Posted 2 years ago #

    Let me tell you why this is a major headache. When you use wp_nav_menu() to create a navigation menu in a theme, it creates a nice menu using all the options you set with regards to classes, IDs, format - it allows you to style it nicely with CSS.

    However,
    If for whatever reason there is no menu defined, WP attempts to create a menu based on a function in the fallback_cb option, the default is wp_page_menu() which generates completely different markup and classes, most of which cannot be edited at this point from the wp_nav_menu(), thus rendering any styles for the proper menu redundant.

    The only way around this is to effectively style your menu twice, or create a new fallback-callback which emulates wp_nav_menu but with he default pages.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    And? Why is this a problem? It's relatively easy to add the extra classes to your CSS.

  3. Dunhamzzz
    Member
    Posted 2 years ago #

    It may be to some extent, but picture this scenario:

    // Creating a menu with the most default of settings:

    // header.php
    wp_nav_menu( array('theme_location' => 'my_theme_top_menu') );
    
    // functions.php
    register_nav_menus(
    	array('my_theme_top_menu' => 'Top Menu' )
    );

    Here's the markup produced with a menu defined:

    <div class="menu-main-menu-container">
        <ul id="menu-main-menu" class="menu">
            <li id="menu-item-102" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-102">
                <a href="http://www.domain.co.uk/">Home</a></li>

    Notice lots of useful class names and IDs on the menu items, and the .menu class on the <ul> tag. Now take a look at what happens if you don't define a menu in wp-admin:

    <div class="menu">
        <ul>
            <li class="page_item page-item-51">
                <a href="http://www.domain.co.uk/bligs/">About Us</a>

    All the CSS has completely changed, what really throws a spanner in the works is the .menu class is now on the outer <div> instead of the the <ul>, and there is no way to pass optinos to wp_page_menu through wp_nav_menu. It's an absolute mess consistency wise.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    So create a custom callback for wp_nav_menu().

  5. Dunhamzzz
    Member
    Posted 2 years ago #

    That is what I'm doing as I need to get this task done across 10 different themes.

    But this basically means that everyone who has ever (properly) made a theme is having to directly emulate some of the WordPress core, and this really should not have to be done, surely you see that?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    I've made dozens of themes and I've not had any problems with this myself. I think I've only used custom callback once or twice where I wanted to provide backwards compatibility.

  7. Dunhamzzz
    Member
    Posted 2 years ago #

    Well you've either made very simple themes or not tested them properly with and without custom menus. I am sitting here with 10 themes with CSS drop downs. They all worked fine until I de-selected the menu and WP "default" kicked in, which is actually not very default at all.

  8. Dunhamzzz
    Member
    Posted 2 years ago #

  9. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Well you've either made very simple themes or not tested them properly with and without custom menus.

    I beg your pardon! That's some assumption! FWIW, four of them are in the Theme Repo, so not only have I tested them thoroughly with (and without) custom menus) but so will have at least one theme reviewer.

    If you cannot style the menus in both situations, then I suggest that you consider honing your CSS skills before passing judgement on the abilities of others.

  10. Dunhamzzz
    Member
    Posted 2 years ago #

    I checked out your themes, the menus on them are actually pretty basic, but that's besides the point.

    This is not a "help me I'm stuck" post, but a "WTF why does this do this" post.

  11. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    This is not a "help me I'm stuck" post

    Then drop the personal comments.

    but a "WTF why does this do this" post.

    For backwards compatibility. wp_page_menu() existed long before wp_nav_menu().

  12. Dunhamzzz
    Member
    Posted 2 years ago #

    Well if that was the reason in mind it seems strange that wp_nav_menu() wasn't created to produce something akin to wp_page_menu()

Topic Closed

This topic has been closed to new replies.

About this Topic