Skip to:
Content
Pages
Categories
Search
Top
Bottom

Remove notification count if zero


  • gcrea
    Participant

    @gcrea

    Hi, I’m trying to remove the admin bar notification counter if it is zero, but show it otherwise. I’m using the following code, hope you can help! I have this in my footer file.

    $(function () {
    if (parseInt($(“#wpadminbar span.count”).text()) == 0) {
    //$(“.notification-counter”).hide();
    $(“#wpadminbar span.count”).hide();
    }
    });

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

  • Venutius
    Moderator

    @venutius

    Hi there,

    I’m not sure where you are getting your classes from, I wrote this which works on my system:

    const notificationsCounter = document.getElementById('ab-pending-notifications');
    const notificationsIndicator = document.getElementById('wp-admin-bar-bp-notifications');
    
    function counterChange() {
    	if (notificationsCounter.innerText == '0') {
    		notificationsIndicator.style.display = 'none';
    	} else {
    		notificationsIndicator.style.display = 'block';
    	}
    }
    
    notificationsCounter.addEventListener('change', counterChange);
    counterChange();
    

    There’s plugins that allow you to get dynamic notification updates, so just in case you ever want to add that feature in future I made it so that it can dynamically change.


    gcrea
    Participant

    @gcrea

    Hi, thanks so much but it doesn’t seem to like the top two lines:

    const notificationsCounter = document.getElementById('ab-pending-notifications');
    const notificationsIndicator = document.getElementById('wp-admin-bar-bp-notifications');

    I am getting the following php error:

    Parse error: syntax error, unexpected ‘(‘, expecting ‘,’ or ‘;’ in C:\wamp\www\models\wp-content\themes\models\functions.php on line 405

    Line 405 is the first line.


    Venutius
    Moderator

    @venutius

    This is not PHP, it’s Javascript, I assumed you’d put it in your footer between some script tags since that’s what you were doing with the jquery.


    Venutius
    Moderator

    @venutius

    One aspect I forgot was if the user is not logged in, ths would fix that:

    const notificationsCounter = document.getElementById('ab-pending-notifications');
    const notificationsIndicator = document.getElementById('wp-admin-bar-bp-notifications');
    
    function counterChange() {
    	if (notificationsCounter.innerText == '0') {
    		notificationsIndicator.style.display = 'none';
    	} else {
    		notificationsIndicator.style.display = 'block';
    	}
    }
    
    if (notificationCounter){
        notificationsCounter.addEventListener('change', counterChange);
    }
    counterChange();
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar