Tagged: code, member type
Apologies if this is addressed elsewhere already.
Regarding the Member Type plugin for BuddyPress I am looking for guidance on how to display the member type as a label next to the user name in both the Member Directory and on the Member’s Profile page.
Is there a specific function to insert into my templates?
Thanks in advance.
Darrin
Hi Darrin,
Ravi is on leave, I am going to help you with this.Please put the following code in your bp-custom.php
/** * Print the list of member types of the user in member directory. */ function buddydev_show_member_type_labels() { $user_id = bp_get_member_user_id(); if( ! $user_id ) { return ;// It should never happen but in case someone put the ac } $member_types = bp_get_member_type( $user_id, false ); if ( ! $member_types ) { return; } $labels = array(); foreach ( $member_types as $member_type ) { $mtype_object = bp_get_member_type_object( $member_type ); if ( ! $mtype_object ) { continue; } $labels[] = $mtype_object->labels['singular_name']; } echo join( ', ', $labels ); } add_action( 'bp_directory_members_item', 'buddydev_show_member_type_labels' );
It will work for single/multiple both.
You can modify it as you please.
Hope that helps.
Regards
BrajeshWe will need to rewrite that. I will break it into two functions and post back today.
Thank you
brajeshHi Darrin,
Please remove the old code and use this. It shows for both the displayed user as well as the directory item./** * Get an array of member type labels based on member types. * * @param array $member_types member types. * * @return array */ function buddydev_get_member_type_labels( $member_types ) { $labels = array(); if ( ! $member_types ) { return $labels; } foreach ( $member_types as $member_type ) { $mtype_object = bp_get_member_type_object( $member_type ); if ( ! $mtype_object ) { continue; } $labels[] = $mtype_object->labels['singular_name']; } return $labels; } /** * Print the list of member types of the user in member directory. */ function buddydev_show_member_type_labels() { $user_id = bp_get_member_user_id(); if ( ! $user_id ) { return;// It should never happen but in case someone put the ac } $labels = buddydev_get_member_type_labels( bp_get_member_type( $user_id, false ) ); echo join( ', ', $labels ); } add_action( 'bp_directory_members_item', 'buddydev_show_member_type_labels' ); function buddydev_show_member_type_for_displayed_user() { if ( ! bp_is_user() ) { return; // just a safeguard, it should never happen. } $user_id = bp_displayed_user_id(); $labels = buddydev_get_member_type_labels( bp_get_member_type( $user_id, false ) ); echo join( ', ', $labels ); } add_action( 'bp_member_header_actions', 'buddydev_show_member_type_for_displayed_user' );
Hope that helps.
Brajesh
You must be logged in to reply to this topic.