I have the member types pro plugin, and I’m looking to display the member types associated with a user along with the avatar of the member type. How can I get the avatar URLs for the member types associated with a user?
Hi Timothy,
Thank you for using BuddyPress Member Types pro.In order to assist you, I will need a little more details.
Question:- Where do you want to show the member type with avatar? On directory page? or using shortcode or on single users profile?
Please let me know and I will assist.
Regards
BrajeshHi Brajesh! Thanks for the response.
I would like to show the member type avatar within a shortcode. I’m building a shortcode and I have retrieved the list of member types by using
bp_get_member_types( array(), 'objects' );
.I was thinking I’d have to use
bp_core_fetch_avatar()
, but I don’t see anitem_id
to use here.Hi Timothy,
At the moment, we don’t have an API for retrieving the Avatar URL for member type. It is an isolated module.There is a way to get the avatar url efficiently(there are other option but that does not uses proper caching).
I wrote a custom function that you may use
/** * Get avatar associated with a member type. * * @param string $member_type member type. * @param string $thumb_type thumbnail type(thumb|full). * * @return string */ function mtp_custom_get_avatar_url( $member_type, $thumb_type = 'thumb' ) { $mtp_object = bpmtp_get_member_type_entry( $member_type ); if ( ! $mtp_object ) { return ''; } $post_id = $mtp_object->post_id; if ( ! $post_id ) { return ''; } $avatar_url = ''; switch ( $thumb_type ) { case 'thumb': $avatar_url = get_post_meta( $post_id, '_bp_member_type_associated_avatar_thumb_url', true ); break; case 'full': $avatar_url = get_post_meta( $post_id, '_bp_member_type_associated_avatar_full_url', true ); break; } return $avatar_url; }
Once you have put it in your theme’s functions.php or bp-custom.php in wp-content/plugins. you can use it to get the url of the avatar image.
Hope that helps.
Regards
Brajesh- This reply was modified 5 years, 9 months ago by Brajesh Singh. Reason: Code updated
EDIT:
Didn’t see that you had posted a reply already. As you mentioned other versions don’t use caching, which is probably the version I posted below. I will go with your function, thank you!!
I ended up finding the
class-bpmtp-avatar-helper.php
file! I saw that in there you guys useget_post_meta
. So I did something like this:$member_types = get_posts( array( 'post_type' => 'bp-member-type', 'numberposts' => -1 ) ); foreach ( $member_types as $member_type ) { $avatar_full_url = get_post_meta( $member_type->ID, '_bp_member_type_associated_avatar_full_url', true ); }
- This reply was modified 5 years, 9 months ago by Timothy Fisher.
- This reply was modified 5 years, 9 months ago by Timothy Fisher.
Hi Timothy,
Your code will work fine. By using my previous suggestion, you can avoid extra database query for fetching post type entries(We already fetch them for .PS:- Please use my updated code. In the previous code even I used an inefficient internal method.
Regards
BrajeshOops. It seems I have been messing it.
Please keep using your own for now. It does causes double query but is still better than my suggestion.
I will share better code later today.
regards
BrajeshHi Timothy,
Hope you are doing well.Your code will work fine. If you want further improvement and saving some query, you may use
$mtp_objects = bpmtp_get_active_member_type_entries();
It returns an array of BPMTP_Member_Types_Pro_Entry. Each contains a post_id. You can use the post id to fetch avatar.
This will save double queries as the member types are cached per request.
Regards
Brajesh
The topic ‘ [Resolved] How to get the avatar associated with a member type?’ is closed to new replies.