首页 > 解决方案 > 气泡通知 Wordpress - 如果 0 则隐藏 / 如果 +1 则显示

问题描述

我是 php 和 css 的新手。

经过大量阅读和很少的学习后,我设法在我的项目中用 php 和 css 编写通知。它工作正常,显示数量(朋友、消息和通知)并重定向到应有的位置。

我的问题是,当用户没有通知时,我需要“隐藏”“0”的气泡,只有在他们有 1 个或多个通知时才显示

函数.php

if ( !is_user_logged_in()) {
    return $menu;
}

$user_id = bp_loggedin_user_id();
$user_url = bp_loggedin_user_domain();

$friends_url = $user_url . bp_get_friends_slug() . '/';
$msg_url = $user_url . bp_get_messages_slug() . '/';
$notify_url = $user_url . bp_get_notifications_slug() . '/';

ob_start();

<li><a><a href="<?php echo bp_loggedin_user_domain();?>friends"><span class="notifications_icons"><i class="fas fa-user-friends" style="font-size:17px;"></i></span><span class="my_bubble_notify"><?php echo friends_get_friend_count_for_user( $user_id );?></span></a></a></li>

<li><a><a href="<?php echo bp_loggedin_user_domain();?>messages"><span class="notifications_icons"><i class="fas fa-envelope" style="font-size:17px;"></i></span><span class="my_bubble_notify"><?php echo bp_get_total_unread_messages_count( $user_id );?></span></a></a></li>

<li><a><a href="<?php echo bp_loggedin_user_domain();?>notifications"><span class="notifications_icons"><i class="fas fa-bell" style="font-size:17px;"></i></span><span class="my_bubble_notify"><?php echo bp_notifications_get_unread_notification_count( $user_id );?></span></a></a></li>
<?php
    $menu_items = ob_get_clean();

    $menu = $menu . $menu_items;
    return $menu;
}

add_filter( 'wp_nav_menu_items', 'my_counter_nav_menu' );
?>

CSS

.notifications_icons {
color: #ffffff;
vertical-align: middle;
margin: -7px;
}

.my_bubble_notify {
color: #ffffff;
border-radius: 3px;
background-color: #f7823f;
vertical-align: 20%;
position: relative;
    font-size: small;
    padding: 0.1em 5px;
}

Buddypress 6.0.0

WordPress 5.4.2

网站:http ://www.gamerstoop.com

如果需要,可以使用测试账户(acc:test – pw:test)

我不知道我是否可以在这里粘贴我的网站链接。如果我不能请删除它或让我知道,非常感谢您阅读和帮助我!<3

标签: phpcsswordpressnotifications

解决方案


我相信如果您将 my_bubble_notify 跨度包装在 if 语句中,它应该可以工作。

所以例如。

 <li>
    <a>
       <a href="<?php echo bp_loggedin_user_domain();?>friends">
          <span class="notifications_icons">
            <i class="fas fa-user-friends" style="font-size:17px;"></i>
          </span>
         <?php if(friends_get_friend_count_for_user( $user_id ) >= 1) { ?>
           <span class="my_bubble_notify"><?php echo friends_get_friend_count_for_user( $user_id );?></span>
         <?php } ?>
       </a>
    </a>
</li>

推荐阅读