【问题标题】:Bubble Notification Wordpress - Hide if 0 / Show if +1气泡通知 Wordpress - 如果 0 则隐藏 / 如果 +1 则显示
【发布时间】:2020-06-22 14:57:59
【问题描述】:

我是 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.gamerstroop.com

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

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

【问题讨论】:

    标签: php css wordpress notifications


    【解决方案1】:

    我相信如果您将 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>
    

    【讨论】:

    • 解决了!非常感谢@kandi_galaxy 如果有人需要它的功能:bp_friend_get_total_requests_count bp_get_total_unread_messages_count bp_notifications_get_unread_notification_count
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多