0

How can I add a custom < li>-element to wp_nav_menu()? My menu includes already all my categories, but it should also include a < li>-element that contains a search form.

From header.php

    <nav>
      <?php
      $args = array(
        'theme_location' => 'nav_main'
        );

      wp_nav_menu($args);
     ?>
     <?php get_search_form(); ?>
    </nav>

--Edit--

This is how it looks now:

enter image description here

This is how it should look:

enter image description here

  • can you please share the code? – Milan Bastola May 11 '18 at 20:37
  • What code exactly? – bodemiller1 May 11 '18 at 20:47
  • Are you wanting to add it at the beginning or end? If so you can use the items_wrap parameter to just inject it. – Howdy_McGee May 11 '18 at 20:52
  • it will be best if you include WordPress nav menu code and output code as well. – Milan Bastola May 11 '18 at 20:55
  • I added two screenshots. – bodemiller1 May 11 '18 at 20:58
1

The wp_nav_menu() function has an items_wrap parameter which contains this by default:

<ul id="%1$s" class="%2$s">%3$s</ul>

So you can pass it the above default plus any appended string:

$search_form = get_search_form( false ); // Return not echo

$items_wrap = '<ul id="%1$s" class="%2$s">%3$s';
$items_wrap .= sprintf( '<li id="searchItem">%1$s</li></ul>', $search_form );

wp_nav_menu( array(
    'items_wrap' => $items_wrap,
) );

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.