Home Forums Front-end Issues How to get the label and value of checkbox ?


How to get the label and value of checkbox ?

  • Hello!

    I checked documentation for checkbox and I also searched forum but I could not find how to get value and label from checkbox.

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

    I will like to show both on front-end.

    This is my code:

    <ul id="filterOptions">
    			<?php while ( have_rows('gallery') ) { the_row(); ?> 
    					$field = get_sub_field_object('category');
    					$value = $field['value'];
    					$choices = $field['choices'];
    						 if( $value ) {
    							 foreach( $value as $v ) { 
    								echo $choices[ $v ]; 
    			<?php	 } ?>
  • You’re echoing the label but not the value, you just need to echo $v in your code

    if( $value ) {
    	foreach( $value as $v ) { 
    		echo $v,':',$choices[ $v ]; 
  • Thanks for suggestion but in this case I’m actually getting all selected checkboxes and I will actually like just to list all options.

    I’m not sure how to do this since I actually have checkboxes inside repeater field.

  • If you want to list all options then just loop through $field['choices']

    foreach ($field['choices'] as $choice_value => $choice_label) {
      echo $choice_label,': ';
      if (in_array($choice_value, $value)) {
        echo 'Selected';
      } else {
        echo 'Not Selected';
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘How to get the label and value of checkbox ?’ is closed to new replies.