menus - Reverse the order of <li> and <a> in a wp_nav_menu

admin2025-06-05  3

I am trying to invert the order of <li> and <a> in a wp_nav_menu, since for a responsive mobile design I like more when the click is done in all the <li> and not only in the word / s of the link <a> because is easier for the finger. Thank you. PS: I must say that I am a newbie in Wordpress development.

This is the code I use:

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

And the answer I get is:

<div id="header-menu-mobile-nav">
    <nav>
        <ul id="header-menu-mobile-nav-ul">
            <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-181">
                 <a href="index.php">Home</a>
            </li>
        </ul>
     </nav>
</div>

What I want is:

<div id="header-menu-mobile-nav">
     <nav>
          <ul id="header-menu-mobile-nav-ul">
               <a href="index.php">
                    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-181">Home</li>
               </a>
          </ul>
     </nav>
</div>

I am trying to invert the order of <li> and <a> in a wp_nav_menu, since for a responsive mobile design I like more when the click is done in all the <li> and not only in the word / s of the link <a> because is easier for the finger. Thank you. PS: I must say that I am a newbie in Wordpress development.

This is the code I use:

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

And the answer I get is:

<div id="header-menu-mobile-nav">
    <nav>
        <ul id="header-menu-mobile-nav-ul">
            <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-181">
                 <a href="index.php">Home</a>
            </li>
        </ul>
     </nav>
</div>

What I want is:

<div id="header-menu-mobile-nav">
     <nav>
          <ul id="header-menu-mobile-nav-ul">
               <a href="index.php">
                    <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-181">Home</li>
               </a>
          </ul>
     </nav>
</div>
Share Improve this question edited Dec 16, 2018 at 21:33 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 16, 2018 at 21:30 user157104user157104 1
  • Note that the desired HTML you've specified is invalid. I take it you're trying to make the entire menu item clickable, not just the text? If so then this is not the solution, and the solution can be found entirely via CSS. If you got WP to output this, browsers would try to correct the DOM and it would not be what you ended up with – Tom J Nowell Commented Dec 17, 2018 at 0:29
Add a comment  | 

2 Answers 2

Reset to default 2
<ul>
    <a>
        <li></li>
    </a>
</ul>

Is not valid HTML. An <a> tag cannot be a child of a <ul> tag. So what you're asking for isn't possible. It's not your actual problem either.

Your problem is a styling problem. If you want a larger touch target on the link, you need to add padding around the <a> tag, not the <li> tag.

Finally I have kept the correct HTML format:

<ul>
     <li>
         <a></a>
     </li>
</ul>

And in CSS I put the element <a> with display: bock; and the padding that I want, and therefore occupies 100% of the elementu <li>. Thank you.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749091729a316291.html

最新回复(0)