Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hide Notification Count When Count is 0


  • Tom Swain
    Participant

    @visiondigitalaz

    I’m building a notification dropdown outside of the logged-in users profile on a sidebar.

    I’m using this line of code to get the count

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

    The notification count does appear, and for the correct user, but does not go away once the count is 0.

    How can I get the count to display:none when the count is 0?

    This is my first ever support post on any platform, so please take it easy on me if I didn’t include all the proper information. Thanks!

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

  • raviousprime
    Participant

    @raviousprime

    Hello,

    you can do something like. It will print nothing when got 0.

    $count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    $count = empty( $count ) ? '' : $count;

    and use this variable in your code.

    Regards
    Ravi


    Tom Swain
    Participant

    @visiondigitalaz

    Thank you so much for your reply!

    If I’m understanding you correctly, I put the code in like this…

    
    <?php
    	$count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    	$count = empty( $count ) ? '' : $count;
    ?>
    

    But that doesn’t seem to be working. Am I doing something wrong?


    Tom Swain
    Participant

    @visiondigitalaz

    For anyone else trying to figure this out, I didn’t find THE solution, but I did find A solution… it’s simple javascript. Wrap your notification in a div with the class “count” or whatever works for you, and hide it when its content is ‘0’.

    
    <div class="count">
    	<?php echo bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ); ?>
    </div>
    
    
    <script type="text/javascript">
    	//HIDE NOTIFICATION IF COUNT IS ZERO
    	let divs = document.getElementsByClassName('count');
    
    	for (let x = 0; x < divs.length; x++) {
    	    let div = divs[x];
    	    let content = div.innerHTML.trim();
    
    	    if (content == '0') {
    	        div.style.display = 'none';
    	    }
    	}
    </script>
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar