Hide count – please help a PHP newbie
-
Hi there,
I’m developing an intranet site for a client – I’m (very) new to the world of PHP, so I’m struggling with getting things customised with the buddypress part of the site. I’m after some pointers for hiding a notifications/new messages count if it’s 0. I’ve managed to get these showing in my user menu using the following code:
<?php // hacks and mods will go here 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 class="notify"><a href=" ' .$friends_url. ' ">Connections <div class="friendnumber topnavcount">'.friends_get_friend_count_for_user( bp_loggedin_user_id() ) .'</div></a></li> <li class="notify"><a href=" ' .$notif_url. ' ">Notifications <div class="notifynumber topnavcount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ).'</div></a></li> <li class="notify"><a href=" ' .$msg_url. ' ">Messages <div class="messagenumber topnavcount">'.bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .'</div></a></li> '; $menu = $menu . $notif; return $menu; } add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' ); ?>
My first issue was that these new menu items were showing up on both the main nav and the user menu of the site, but I’ve fixed that by applying the classes shown and hiding them using CSS (something I’m much more familiar with!)
What I need is someone kind to spell out exactly what I need to do to hide the counts for notifications and unread messages above if they’re 0. The friends bit I’m not so worried about as I may either change how that works or drop it entirely (its a corporate client). I’ve already tried a number of javascript options to remove the div if the content is 0… but guess what, I’m as great at javascript as I am at PHP!
Thanking everyone here in advance – it was only by searching these forums that I was able to get this far, so I appreciate all the effort everyone goes to to help people like me out!
John
-
Try:
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; $notif = '<li class="notify"><a href=" ' .$friends_url. ' ">Connections <div class="friendnumber topnavcount">'.friends_get_friend_count_for_user( bp_loggedin_user_id() ) .'</div></a></li>'; if ( bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) != 0 ) $notif .= '<li class="notify"><a href=" ' .$notif_url. ' ">Notifications <div class="notifynumber topnavcount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ).'</div></a></li>'; if ( bp_get_total_unread_messages_count( bp_loggedin_user_id() ) != 0 ) $notif .= '<li class="notify"><a href=" ' .$msg_url. ' ">Messages <div class="messagenumber topnavcount">'.bp_get_total_unread_messages_count( bp_loggedin_user_id() ) .'</div></a></li>'; $menu .= $notif; return $menu; } add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
Wow, this is great Shane, thank you very much – and so close to what I’m trying to achieve, but just not quite right. When updated to the supplied, the count disappears when it is 0, but so does the link to the messages/notifications as well – is there a way to just make the count number itself disappear rather than the whole thing?
Thanks so much again, wonderful support
if ( bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) != 0 ) $notif .= '<li class="notify"><a href=" ' .$notif_url. ' ">Notifications <div class="notifynumber topnavcount">'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ).'</div></a></li>'; else $notif .= '<li class="notify"><a href=" ' .$notif_url. ' ">Notifications</a></li>';
Absolutely brilliant – thank you so much again. This has sorted me out with something I had been stubbornly trying to fix alone for a frustrating few days. Thank you so much again!
John
- The topic ‘Hide count – please help a PHP newbie’ is closed to new replies.