Xprofile get field data checkbox returns array
-
Hi
I am using the following to get a members xprofile data displayed
<?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id()) ?>
It works fine for xprofile fields that are free text, but the fields that are checkboxes, it simply returns ‘array’ to the front end.
Is there a way to include this so it works?
Thanks,
Ian
-
Can anyone help with this?
Thanks
Really trying to get this one sorted, can’t find a fix – anybody know?
This is expected, because the function’s not intended to be used like this (for direct output to HTML).
Are you trying to do this in a WordPress template — looks like on a author archive page template?
I See.
Screenshot is here of what we are doing:
I am trying to use it in a sidebar widget on a post page. Using the following PHP in the text widget to get back users info:
<div class="<?php echo get_post_meta(get_the_id(), 'Account Type', true); ?>"> <a href="/community/members/<?php echo get_the_author_meta('user_login') ?>"> <?php echo get_avatar ( get_the_author_meta('user_email'), $size = '192' ); ?> </a> <a href="/community/members/<?php echo get_the_author_meta('user_login') ?>"><h2 class="profileHeader"> <?php echo get_the_author(); ?></h2></a> <h2 class="profileAccount"><?php echo get_post_meta(get_the_id(), 'Account Type', true); ?></h2> <h3>Specialist Sectors</h3> <?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id()) ?> <h3>Experience</h3> <?php echo xprofile_get_field_data( 'Experience', get_the_author_id()) ?> <h3>About</h3> <?php echo xprofile_get_field_data( 'About you', get_the_author_id()) ?>
Thanks,
Ian
To return a csv string rather than array, try:
xprofile_get_field_data( 'Specialist Sectors', get_the_author_id(), $multi_format = 'comma' )
With checkboxes you’re not storing a single item of data as the user can check many checkboxes. This is why the the data is stored in an array. If you want to display the list of values you can easily process the array with a for loop, echoing the value on each cycle.
Or of course use shanebpdev’s approach if you need a comma separated list
just fyi: the data is not stored in an array.
It is stored as a serialized string.
Depending on the multi_format value, that string can be returned as an array (and handled in a loop) or as a csv string.boone was kind enough to add the multi_format var awhile back so that we don’t always need to check type on the returned value in customized displays.
thanks @shanebp, nice one for explaining it all properly :}
Thanks all… @shanebp I’ve inputted the following but it isn’t showing anything, is my implementation incorrect?
<?php echo xprofile_get_field_data( 'Specialist Sectors', get_the_author_id(), $multi_format = 'comma' ) ?>
Thanks
echo get_the_author_id() first to make sure it has a value.
If it doesn’t, xprofile_get_field_data will return nothing.
also – If the profile field in question is empty, nothing will be returned.
OK thanks…so I have switched it around as follows:
<?php echo get_the_author_id(), xprofile_get_field_data( 'Specialist Sectors', $multi_format = 'comma' ) ?>
I am now getting something returned, which is unique for each user but it’s returning a number, such as 52, 65 or 1 – I don’t know what these refer to.
Any ideas?
That doesn’t look quite right to me. I’ve never used the $multi_format approach myself so perhaps shanebpdev can advise on that. My approach would be this:
<?php if ( $sectors = bp_get_profile_field_data( 'field=Specialist Sectors' ) ) { ?> <?php foreach ( $sectors as $sector ) { ?> <?php echo $sector; ?> <?php } ?> <?php } ?>
Unfortunately that is returning blank too @henrywright-1
Thanks
May I ask where in your theme you’re using the code?
Also, double check the exact spelling you’ve used for the field name matches “Specialist Sectors”. Also try editing your profile making sure a few checkboxes have actually been checked. Sometimes it is the most obvious parts of the process that cause the issue.
Hi there
I’m using it in a text widget sidebar, which works for all the other fields.
The spelling is correct and there are multiple options selected.
Thanks,
Ian
Try using the code in your user profile template, then if that works you’ll know the widget is the issue.
Yes it works on profiles.php… but the text widget I use on a post page works for all the other xprofile fields?
Can you paste the exact code which you are using in the text widget on the post page that works, and can you then paste the code you are using in the post page widget that does not work?
Hi there
In the page template test I used your code exactly, within the profile.php file.
In the text widget I use the following (all of which work except Specialist Sectors):
`<div class=”<?php echo get_post_meta(get_the_id(), ‘Account Type’, true); ?>”>
“>
<?php if ( $sectors = bp_get_profile_field_data( ‘field=Specialist Sectors’ ) ) { ?>
<?php foreach ( $sectors as $sector ) { ?>
<?php echo $sector; ?>
<?php } ?>
<?php } ?>
“><h2 class=”profileHeader”> <?php echo get_the_author(); ?></h2>
<h2 class=”profileAccount”><?php echo get_post_meta(get_the_id(), ‘Account Type’, true); ?></h2>
<h3>Specialist Sectors</h3><h3>Telephone Number</h3>
<?php echo xprofile_get_field_data( ‘Telephone Number’, get_the_author_id()) ?>
<h3>Email</h3>
“><?php echo xprofile_get_field_data( ‘Contact Email Address’, get_the_author_id()) ?>Thanks for pasting everything. I see now exactly what you’re trying to do. This should work:
<?php if ( $sectors = xprofile_get_field_data( 'Specialist Sectors', get_the_author_id() ) ) { ?> <?php foreach ( $sectors as $sector ) { ?> <?php echo $sector; ?> <?php } ?> <?php } ?>
Perfect!
Thanks SO much.
- The topic ‘Xprofile get field data checkbox returns array’ is closed to new replies.