T
T
Timur Khudiyev2016-12-07 17:19:35
User navigation
Timur Khudiyev, 2016-12-07 17:19:35

How to add custom item to menu in wordpress?

The point is this. I would like to add my li to the end in the menu, in the frontend, in order to place a search string in it. I found one solution in the function file, everything works, but at the bottom of the site there is another menu where the last item should be different, and this search is added there too. looked at this function, but could not find any menu identifier there, in which you need to add an extra item.
Here is the code from function.

add_filter('wp_nav_menu_items','add_search_box', 10, 2);
  function add_search_box($items, $args) {
    ob_start();
    get_search_form();
    $searchform = ob_get_contents();
    ob_end_clean();
    $items .= '<li class="menu-item">' . $searchform . '</li>';
    return $items;
  }

Of course, you can use css after, but you don’t want to write an entire search form in the css file. Well, or at worst, you have to insert it through js.
If what other method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mekeda, 2018-06-04
@mekeda

Also faced this problem. Decided to just hide the form in the footer using css

.footer .menu-item{
  visibility: hidden;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question