0

I have to add a bubble notification in WordPress wp_nav_menu like the following code.

<ul>           
      <li> Tickets <span class="unread">2</span></li>

        <li> Log-out</li>
</ul>

Here I have to add this part in it.

<span class="unread">2</span>

I have seen this one and tried it.

add_action( 'walker_nav_menu_start_el', 'wpse_10042_nav_menu_post_count', 10, 4 );

 function wpse_10042_nav_menu_post_count( $output, $item, $depth, $args ) {

    $output .= '<span class="unread">'.my_function_here().'</span>';

    return $output;
}

And get it from here How to add post count to wp_nav_menu?

But the problem is its adding to every menu link. I want to add it to only one menu item. Not everyone in the list.

3

For flexibility, you could assign the CSS bubblecount class to the corresponding menu item:

enter image description here

and then target it with:

if( in_array( 'bubblecount', (array) $item->classes ) )
    $output .= '<span class="unread">'.my_function_here().'</span>';

in your code snippet above.

  • can you tell me little elaborately. ? I am not much experienced in it – Kvvaradha Nov 24 '15 at 13:18
  • sure, please check out the update @Akelya – birgire Nov 24 '15 at 13:21
0

How about just

function add_bubble_to_nav( $items, $args ) {
    $items .= '<span class="unread">stuff</span>';
    return $items;
}
add_filter( 'wp_nav_menu_items', 'add_bubble_to_nav', 10, 2 );

No need to use walker here, just go for the filter I think.

This adds it to the end of the menu. If you need it in a specific position you could go for strpos to find the position and then substr_replace to put it in there or something.

  • Thank you for your code. But it adds the stuff at the end of menu list. i want to add it to single menu item. any idea, how to make it work on single menu only – Kvvaradha Nov 24 '15 at 13:15
  • Would need to know a little more about the menu's structure. But should be doable with strpos and substr_replace, just find the position where you want it and insert it in the $items at the wanted place. – jimihenrik Nov 24 '15 at 13:21

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.