Xprofile field length control

de | 16 juillet 2014

Imaginons que vous avez un champ de profil contenant une biographie ou une présentation quelconque et que vous voulez l’afficher, au moins sous forme d’extrait, sur l’annuaire des membres.
Let’s imagine you have an xprofile texfield containing a biography which you want to display, even partially, on the members directory.

function bpfr_custom_textfield_length() {	
	//Check if user is logged in & if xprofile component is activated
	if ( is_user_logged_in() && bp_is_active( 'xprofile' ) ) :	
		$my_custom_textfield = bp_get_member_profile_data( 'field=Brief Biography&user_id='.bp_get_member_user_id() );	
	 * The length = number of characters, not words.
	 * Set the number of caracters to show. 
	 * The 3 dots are the appended text ending the excerpt.
	 * Don't remove the quotes if you change this
	 * BuddyPress 2.1 will add new class and id for custom fields. 
	 * The span can be omited to style this part. See ticket #5741
		if ( strlen($my_custom_textfield) > 20) :  //adjust to your need
			$my_custom_textfield = substr($my_custom_textfield, 20).'...'; //adjust to your need
			// uncomment the following line to get a span around the displayed field content
		// echo '<span class="short-custom">'. $my_custom_textfield; .'</span>';
			// comment the following line if you use the span
		echo $my_custom_textfield;

	endif; // is_user_logged_in
add_action( 'bp_directory_members_item', 'bpfr_custom_textfield_length' );
Partagez...Tweet about this on TwitterShare on Google+Share on FacebookEmail this to someone