1

I found code to show a notification bar on menu and customize it a little. It is working and shown on main menu (primary menu) and footer menu (secondary menu) but I want to show it only main menu. How to do that? Here's what I have:

function my_counter_nav_menu( $menu ) {  
    $notif_url   = bp_core_get_user_domain( bp_loggedin_user_id() ) . 'notifications/';
    $friends_url = bp_core_get_user_domain( bp_loggedin_user_id() ) . 'friends/'; 
    $msg_url     = bp_core_get_user_domain( bp_loggedin_user_id() ) . 'messages/';    

    if ( ! is_user_logged_in() ) {
        return $menu;
    } else {
        $notif = '<li><a href=" ' . $notif_url . ' "> <i class="fa fa-bell-o" aria-hidden="true"></i> Notifications [' . bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) . ']</a></li>';
    }

    $menu = $menu . $notif;

    return $menu;
}
add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
2

The wp_nav_menu_items filter has a secondary parameter that it passes: $args. What we need to do is set up our filter to accept second args by passing a priority 10 and the number of args 2:

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

Now that we're passing args - if we peek into what it holds we see the following object:

stdClass Object
(
    [menu] => Menu Name
    [container] => nav
    [container_class] => 
    [container_id] => mainNav
    [menu_class] => menu
    [menu_id] => 
    [echo] => 1
    [fallback_cb] => wp_page_menu
    [before] => 
    [after] => 
    [link_before] => 
    [link_after] => 
    [items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
    [depth] => 0
    [walker] => 
    [theme_location] => 
)

So if we wanted to in your function we can test against either $args->menu or $args->theme_location:

if( 'Menu Name' === $args->menu ) {
    /* .. Run Code Here .. */
}

Or against the location:

if( 'primary-menu' === $args->theme_location ) {
    /* .. Run Code Here .. */
}
0

Hi thx for your answering ,but i confused shall I define one of stdclass objects like [menu] => Menu Name or [theme_location] => İf so,Is it neccessary to change Menu Name which on you typed [menu] => Menu Name Could u send me hole ordered codes,pls?

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.