Help Showing Only Member Uploaded Avatars

Hi guys, I wonder if one of you could kindly help me here please? Basically, I’m using the code below to randomly show 5 Avatar profile images on my BuddyPress site – This all works just fine. However, I ONLY want to show the Avatars of members who have bothered to upload their own picture, and not show my themes default image within the Avatars that get returned.

Any ideas how I can modify the code to show only member uploaded avatars?

<?php if ( bp_has_members( 'type=random&max=5' ) ) : ?>
<?php while ( bp_members() ) : bp_the_member(); ?>
<a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&width=125&height=125') ?></a>
<?php endwhile; ?>
<?php endif; ?>

Thanks for any help
Rob.

  • godrob

    Thanks David,

    Any ideas how I add that to the code above then please?

    I tried changing this line of code:

    <?php if ( bp_has_members( 'type=random&max=5' ) ) : ?>

    to this:

    <?php if ( bp_has_members && bp_get_user_has_avatar ( 'type=random&max=5' ) ) : ?>

    Not sure if that's right as I am not a programmer.

    ..But that just showed a blank area where the Avatars should have appeared. Appreciate your help with this.

    Thanks
    Rob.

  • DavidM

    Hi Rob,

    Sure, just to help you get an idea for what's going on, with the PHP format of the code you posted (I typically use just plain PHP and try not to get mixed up in the <?php ?> tags myself), your "If/Then" tests should look something like follows:

    <?php if ( bp_get_user_has_avatar() ) : ?>
    <?php endif; ?>

    So you basically just insert between those two lines, whatever you'd like displayed, when an uploaded avatar is found for a user.

    And you'd basically just add that check wherever you'd like it in your code, possibly like so:

    <?php if ( bp_has_members( 'type=random&max=5' ) ) : ?>
    <?php while ( bp_members() ) : bp_the_member(); ?>
    <?php if ( bp_get_user_has_avatar() ) : ?>
    <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&width=125&height=125') ?></a>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

    The tricky thing is that since the very first line calls for a maximum of 5 random members, the subsequent check will narrow that down further if those random 5 members don't have avatars uploaded.

    In other words, it may not display 5 member avatars. Of the maximum 5 it randomly pulls, it will only display the avatars of those with uploaded pics.

    Not sure how that'd work for you, but otherwise than that, something like this might work:

    <?php $bp_count_members = 1; ?>
    <?php if ( bp_has_members() ) : ?>
    <?php while ( bp_members() ) : bp_the_member(); ?>
    <?php if ( bp_get_user_has_avatar()  && $bp_count_members <= 5) : ?>
    <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar('type=full&width=125&height=125') ?></a>
    <?php $bp_count_members++; ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

    I've not tested that though, so I'm not really sure how well it'd work and indeed, it would best be done with a bit more thought to be sure it doesn't cause any problems. It's just a rough outline really.

    -David

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.