1

I'm trying to create a theme for wordpress so I've done a clean install. I have not created entries, pages or menus.

At this point I am in the part of creating the menu so I created in my functions.php file the following function:

<?php register_nav_menus( array(
  'menu' => 'Menu superior',)); ?>

And to implement it in my header.php I have implemented it like this:

<!-- Menu -->
<nav id="menu">  
    <h2>Menu</h2>      
    <?php wp_nav_menu(array(
    'container' => false, 
    'items_wrap' => '<ul> %3$s</ul>', 
    'theme_location' => 'menu', 
    )); ?>
</nav>

I realized that when I run it does not give me the style to the elements that it adds to me, these elements are the pages that have been created, in this case is the one that is generated in the installation.Up to that point it looks like this: Sin menu creado manualmente

Then I went to the menu section and created one manually by adding elements. I noticed that the menu appears as I requested:

enter image description here

The question here is, if I provide my theme to users who have a clean installation of wordpress and still do not create a menu, then my menu will be displayed without design as it adds the div container to the menu. And the pages that have been created until that moment would be added, but without style. Would I have to specify to those who acquire it that they first have to create a menu for the style to apply? Or can you make some form so that from the beginning you can see correctly with the style?

I appreciate your help! (An apology, I had to use a translator)

SOLUTION

I have solved it as follows:

    <!-- Menu -->
    <nav id="menu">  
        <h2>Menu</h2>      
        <?php 
        if ( has_nav_menu( 'menu' ) ) {
            wp_nav_menu(array(
                'container' => false, 
                'items_wrap' => '<ul> %3$s</ul>', 
                'theme_location' => 'menu', 
                ));
        }else{
            echo "<ul>";
            wp_list_pages('title_li=');
            echo "</ul>";
        }
        ?>
    </nav>

As mentioned by Milo, I had to check if the menu still does not exist to do a specific action.

1

This is due to the wp_nav_menu parameter fallback_cb:

If the menu doesn't exist, a callback function will fire. Default is 'wp_page_menu'. Set to false for no fallback.

You have not specified an alternative, so it uses the default, wp_page_menu.

You could create your own function to display a page menu with the proper markup, or set the fallback_cb parameter to false and display nothing.

has_nav_menu might also be helpful for displaying alternate markup when a menu isn't available.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.