Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was trying to add a element right after begin of the navigation tags and before the ul will start. So add a element directly after opening nav tag.

I tried this, but this will add a list item after all li's. Maybe we can use it to solve my problem:

add_filter('wp_nav_menu_items', 'add_li_to_nav', 10, 2);

function add_li_to_nav($items, $args)
{
    $items .= '<li>SEARCH</li>';
    return $items;
}

What my goal is, is to add a link <a href="#" "mobile"> right after primary-navwrapper. Like the following markup:

<nav id="primary-navwrapper" class="pushy-right">

<!-- SO HERE RIGHT AFTER NAV PLACE THE FOLLOWING ANCHOR TAG -->
<a href="#" "mobile">


<ul id="listnav" class="menu">

<li id="menu-item-160" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-160">
    <a href="">Fotografie</a>
</li>

</ul>
</nav>
share|improve this question
    
Sorry, you are trying to add a class? What you have done is add whole list item. And what does "before the ul will start" mean? Your question is not clear. Please add some more detail, particularly about the required output. –  s_ha_dum Mar 22 at 14:22
    
See updated version above. Sorry for the unclear question! @s_ha_dum –  Casper Mar 22 at 14:42

1 Answer 1

up vote 2 down vote accepted

The question is unclear but wp_nav_menu() has a couple of arguments that will allow you to add classes to the output. For example...

wp_nav_menu(
  array(
    'container_class' => 'conclass',
    'menu_class' => 'menclass'
  )
);

You can alter those arguments using the wp_nav_menu_args filter as well.

However, I suspect that when you say "class" you are using the wrong term, and what you actually want to do is add a single list item to the beginning of the navigation menu. Your filter is pretty close:

function add_li_to_nav($items, $args) {
  remove_filter('wp_nav_menu_items', 'add_li_to_nav', 10, 2);
  $items = '<li>SEARCH</li>'.$items;
  return $items;
}
add_filter('wp_nav_menu_items', 'add_li_to_nav', 10, 2);

That will add the "search" list item before the current item, and then remove itself so that the "search" list item will not be added in any other locations.

Update based on new information:

By far the simplest solution is going to be to pass wp_nav_menu() a 'container'=>false argument and write the rest into the markup.

<nav id="primary-navwrapper" class="pushy-right">
  <!-- SO HERE RIGHT AFTER NAV PLACE THE FOLLOWING ANCHOR TAG -->
  <a href="#">mobile</a>><?php
  wp_nav_menu(
    array(
      'container' => false
    )
  ); ?>
</nav><?php

Another option is to filter wp_nav_menu:

function add_anchor_to_nav($menu) {
  remove_filter('wp_nav_menu_items', 'add_anchor_to_nav', 10, 2);
  $pat = '(<nav[^>]+?>)(.*)';
  $menu = preg_replace('|'.$pat.'|','$1<a href="#">mobile</a>$2',$menu);
  return $menu;
}
add_filter('wp_nav_menu', 'add_anchor_to_nav', 10, 2);

wp_nav_menu(
  array(
    'container' => 'nav'
  )
);

regex on markup is dicey but this is fairly simple. I think it will perform relatively well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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