我尝试为“未读”通知计数添加 CSS class(颜色)

I try to add CSS class (color) for the “unread” notifications count

我正在尝试为“新的”未读通知设置不同的颜色,虽然我已经设法将通知作为一个整体来设置样式,但我无法以某种方式对未读通知执行相同的操作给未读计数额外 css class.

已经在 BP 社区支持论坛中询问以获得一些提示,但似乎社区在空虚中挣扎。

有人可以帮忙吗?

我的主题中的实际代码 functions.php (WP):

//notification alert
function my_nav_menu_notif_counter($menu) {      
        if (!is_user_logged_in())
                return $menu;
        else
                $notif = '<a class="notices" href="' . bp_get_notifications_unread_permalink() . '"><span class="noticecount"><i class="noticecounttitle">Notifications</i>'. bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ) .'</span></a>';
                $menu = $menu . $notif;
                return $menu;
}
add_filter( 'wp_nav_menu_items', 'my_nav_menu_notif_counter' );

更新: 非常感谢 thunderfury,

//notification alerts for posts
function my_nav_menu_notif_counter($menu) {      
        if (!is_user_logged_in())
                return $menu;
        else
                $countntf = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
                $notif = '<a class="notices" href="' . bp_get_notifications_unread_permalink() . '"><span class="noticecount'. (($countntf>0) ? ' NEW CLASS HERE':'') .'"><i class="noticecounttitle">Notifications</i>'. $countntf .'</span></a>';
                $menu = $menu . $notif;
                return $menu;
}

您的 bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ); 函数 returns 未读计数,您可以将其保存在变量中,例如 $countntf.

以上示例检查返回值是否大于 0,它会在 span 元素中打印您的 class 名称。您也可以将 class 元素跨度更改为 link 元素;

<a class="notices'. (($countntf>0) ? ' NEW CLASS HERE':'') .'"