Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Add Count Near Nav Menu


  • gurselgunacar
    Participant

    @gurselgunacar

    Hello,

    I am trying to find a solution for 2 weeks. Please help me to fix that.

    I want to show message and notification count near nav menu.

    I found that we can show counts of message and notification with following php codes:

    <?php echo bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );?>
    <?php bp_total_unread_messages_count() ?>

    But i couldnt add this codes in menu. I want to change menu like
    Forum Profile Message Notification Contact Blog
    to =>
    Forum Profile (5) Message (12) Notification Contact Blog

    Sorry for that but i dont know php well. My website : http://www.pckopat.net

    Thanks For Your Helps

Viewing 8 replies - 1 through 8 (of 8 total)

  • gurselgunacar
    Participant

    @gurselgunacar

    Please help me to fix this. I couldnt find a solution


    danbp
    Moderator

    @danbp

    Add this to bp-custom.php and give a try ! Should show at the left of the logout button on Buddy theme nav bar

    function my_nav_menu_notif_counter($menu) {  
    
    $url = bp_core_get_user_domain(bp_loggedin_user_id()) .'notifications/';   
           // condition: user must be loggedin
            if (!is_user_logged_in())
                    return $menu;
            else
                    $notif = '<li><a href=" ' .$url. ' ">Notif ['. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .']</a></li>';
    				
                    $menu = $menu . $notif;
                    return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );

    gurselgunacar
    Participant

    @gurselgunacar

    man you are perfect πŸ™‚ it works. Can we add code for messages and friends count?


    danbp
    Moderator

    @danbp

    Yes you can, but why would you do that as all those counters are already in the user menu (on top right, under Howdy) and on each profile ? Tsssss…., but you’re the boss ! πŸ˜‰

    To count friends use friends_get_friend_count_for_user( bp_loggedin_user_id() );
    To count messages use bp_get_total_unread_messages_count( bp_loggedin_user_id() )

    Complete solution

    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. ' ">Notif ['. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .']</a></li>
    <li><a href=" ' .$friends_url. ' ">Friends ['. friends_get_friend_count_for_user( bp_loggedin_user_id() ) .']</a></li> 
    <li><a href=" ' .$msg_url. ' ">Messages ['.  bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .']</a></li>
    
    ';
    				
        $menu = $menu . $notif;
        return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );

    Bu yardΔ±m olabilir ! πŸ˜‰


    gurselgunacar
    Participant

    @gurselgunacar

    @danbp you are my hero. i tried to find this code for 2 weeks πŸ™‚ you solved that. my theme doesnt contain user menu. So i need to add count of notification. Lastly how can we move this menu items left side of menu?


    danbp
    Moderator

    @danbp

    I tested the snippet on Buddy theme, so i’m pretty sure you have a user menu. This menu is on WP’s Toolbar. You see it when you’re logged in.

    counter menu on Buddy theme

    To choose an item position, you have to do it differently, as explained here:

    [Resolved] Position Notification Counter Bubble after Specific Navigation Tab ID

    For more about menu handling and functions, see WP Codex, as this is not really related to BuddyPress. Now it’s your turn to work a little !


    gurselgunacar
    Participant

    @gurselgunacar

    ah you mean admin bar:) i dont want to use that. because i dont want 2 menu on header. You saved my time. Thanks for helping again. i will work on this on code


    awpt
    Participant

    @awpt

    I just tried your function and its working fine but the only problem is that its adding menu items in all wordpress menus.

    Is there anyway to add only in a specific menu instead of all menus?

    Tesekkurler!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Add Count Near Nav Menu’ is closed to new replies.
Skip to toolbar