Support

Account

Home Forums Front-end Issues ACF: How to get the label of a taxonomy Checkbox Field?

Solving

ACF: How to get the label of a taxonomy Checkbox Field?

  • Hi,

    I use a ACF custom field on a custom taxonomy. I want to get the label of the selected checkbox from the acf field “book_type” and use it in a $variable.

    If i use this code, it shows the name of the custom field but not the label, like this: “non_fiction” instead I want to show the label which is called: “Non Fiction”

    <?php the_field('book_type', $term); ?>

    Also I like to use it as a variable instead of echoing directly. So I tested all code I could find with google, like this for example:

    
    $field = get_field_object('field_name');
    $value = get_field('field_name');
    $label = $field['choices'][ $value ];
    
    ?>
    <p>Color: <?php echo $label; ?></p>

    But none of them does the job, how can I get the label of a taxonomy checkbox field with ACF?

  • Just to make sure that I understand.

    You have set up a checkbox field with choices something like:

    
    value_1 : Label 1
    value_2 : Label 2
    value_3 : Label 3
    

    and you want to echo label on the front end rather than the value.

    Do you use the value on the front end or just the label?

  • Yes exactly as you describe. I want to use the Label (front end) in a variable instead of the value. I have tried all code I could find available on the internet, but I didn’t get it working, hope you can help me?

  • 
    $values = get_field('checkbox_field');
    $field = get_field_object('checkbox_field');
    $choices = $field['choices'];
    foreach ($choices as $value => $label) {
      if (in_array($choice, $values)) {
        echo 'LABEL: ',$label,': VALUE',$value,'<br />';
      }
    }
    
  • Hi John,

    I get this error when using your code:

    Warning: Invalid argument supplied for foreach() in ... on line 83

    I used the name of my field: book_type where you used checkbox_field.

    Does it matter that it is a field used on a custom taxonomy? Not on a post.

  • There are two likely causes of this. First is that your field does not have any choices. 2 is that you are using the value outside of the loop. Are you trying to show the selected values for a post or are you trying to show them before the posts are listed?

  • could you try if this works:

    $book_type ='';
    	$book_type_field = get_field_object('book_type');
    	$book_type_values = get_field('book_type');
    	$count = count($book_type_values);
    	foreach ($book_type_values as $key => $value) {
    		$book_type .= $book_type_field['choices'][ $value ];
    		if (($count - 1  - $key) >= 1){$book_type .= ', ';}
    	}
    echo $book_type; //do this whenever you wish (this variable should contain all chosen labels, separated by a comma)
    

    or do you get also a error with this code

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘ACF: How to get the label of a taxonomy Checkbox Field?’ is closed to new replies.