Hide label that toggles comment display unless there are already comments on the activity?

I'm using a code in my function.php file to toggle comment display in the buddypress activity stream. When "show/hide comments" is clicked on, the activity's comments are displayed. Problem is that the "show/hide comments" label is there even when there are no comments on the activity. I would like to change it so that it shows the label only when the activity has comments, otherwise, it shouldn't be there at all.

Here is the code:

/* Show and hide comments */
add_action( 'wp_footer', 'add_comment_hide_show' );

function add_comment_hide_show() {
    ?>
    <style>
    .activity-comments ul{display: none;}
    </style>
    <script type="text/javascript">
    jQuery(function($) {
      setInterval(function() {
        $('.activity-meta').each(function() {
           if( !$(this).find('.show-comments').length ){
            var html = '<a href="#" class="button bp-primary-action show-comments"><i class="fa fa-comments"></i></a>';
            $(this).find('.button.acomment-reply').after(html);
           }
        });

      }, 500);

      $('body').on('click', '.show-comments', function(e) {
        e.preventDefault();

    var obj = $(this).closest('.activity-content').next('.activity-comments').find('ul');
    obj.slideToggle();
        return false;

      });

    });
    </script>
    <?php
}

I believe there should be an if statement along the lines of "if $int_count < 1" .....
but I'm not entirely sure which is why I'm turning to assistance hah. Any help would be appreciated!

  • Sajid

    Hi Alayna,
    Hope you are doing good today :slight_smile:

    I have come up with following code snippet that will detect if there are comments then show the toggle button.

    /* Show and hide comments */
    add_action( 'wp_footer', 'add_comment_hide_show' );
    
    function add_comment_hide_show() {
        ?>
        <style>
        .activity-comments ul{display: none;}
        </style>
        <script type="text/javascript">
        jQuery(function($) {
          setInterval(function() {
            $('.activity-meta').each(function() {
    
               if( !$(this).find('.show-comments').length && $(this).parent().next('.activity-comments').find('ul').length ){
    
                var html = '<a href="#" class="button bp-primary-action show-comments"><i class="fa fa-comments"></i></a>';
                $(this).find('.button.acomment-reply').after(html);
               }
            });
    
          }, 500);
    
          $('body').on('click', '.show-comments', function(e) {
            e.preventDefault();
    
        var obj = $(this).closest('.activity-content').next('.activity-comments').find('ul');
        obj.slideToggle();
            return false;
    
          });
    
        });
        </script>
        <?php
    }

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:
    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.