0

How to hide the Load more Ajax button if there is no more users to load or less than the number

<script type="text/javascript">
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
    $('body').on('click', '.loadmorefollowing', function() {
            var data = 
            {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author': '<?php echo esc_html($curauth->ID); ?>'

            };

    $.post(ajaxurl, data, function(response) {

        $('#following-artists-cont').append(response);
        page++;
    });
    });
});
</script>

1 Answer 1

1

I think something like this may work, if response is just HTML and returns an empty string '' when there are no more results:

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
    var page = 2;
    jQuery(function($) {
        $('body').on('click', '.loadmorefollowing', function() {
            var data = 
                {
                'action': 'user_following_by_ajax',
                'page': page,
                'security': '<?php echo wp_create_nonce("user_more_following"); ?>',
                'author': '<?php echo esc_html($curauth->ID); ?>'

                };

            $.post(ajaxurl, data, function(response) {
                if ( '' === response ) {
                    $('.loadmorefollowing').hide();
                    return;
                }

                $('#following-artists-cont').append(response);
                page++;
            });
        });
    });
</script>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.