Skip to:
Content
Pages
Categories
Search
Top
Bottom

Xprofile get field data checkbox returns array

  • @applegateian

    Participant

    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

Viewing 22 replies - 1 through 22 (of 22 total)
  • @applegateian

    Participant

    Can anyone help with this?

    Thanks

    @applegateian

    Participant

    Really trying to get this one sorted, can’t find a fix – anybody know?

    @djpaul

    Keymaster

    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?

    @applegateian

    Participant

    I See.

    Screenshot is here of what we are doing: screenshot

    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

    @shanebp

    Moderator

    To return a csv string rather than array, try:

    xprofile_get_field_data( 'Specialist Sectors', get_the_author_id(), $multi_format = 'comma' )

    @henrywright-1

    Member

    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.

    @henrywright-1

    Member

    Or of course use shanebpdev’s approach if you need a comma separated list

    @shanebp

    Moderator

    @henrywright-1

    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.

    @henrywright-1

    Member

    thanks @shanebp, nice one for explaining it all properly :}

    @applegateian

    Participant

    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

    @shanebp

    Moderator

    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.

    @applegateian

    Participant

    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?

    @henrywright-1

    Member

    @applegateian

    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 } ?>
    

    @applegateian

    Participant

    Unfortunately that is returning blank too @henrywright-1

    Thanks

    @henrywright-1

    Member

    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.

    @applegateian

    Participant

    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

    @henrywright-1

    Member

    Try using the code in your user profile template, then if that works you’ll know the widget is the issue.

    @applegateian

    Participant

    Yes it works on profiles.php… but the text widget I use on a post page works for all the other xprofile fields?

    @henrywright-1

    Member

    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?

    @applegateian

    Participant

    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()) ?>

    @henrywright-1

    Member

    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 } ?>

    @applegateian

    Participant

    Perfect!Screenshot

    Thanks SO much.

Viewing 22 replies - 1 through 22 (of 22 total)
  • The topic ‘Xprofile get field data checkbox returns array’ is closed to new replies.
Skip to toolbar