Support

Account

Home Forums Add-ons Repeater Field Display a checkbox value within a repeatable field

Solved

Display a checkbox value within a repeatable field

  • Hi there,

    I’m trying to display a checkbox value within a repeatable field. The checkbox allows you to select between White and Black. I want to output this value in a class so that I can style the text depending on the value.

    Here is my code it isn’t working and nothing is returned.

    <? if( have_rows('content_blocks') ){ ?>
      <? while( have_rows('content_blocks') ){
      	the_row();
      // vars
      $background_colour = get_sub_field('background_colour');
      $content_area = get_sub_field('content_area');
      $content_title = get_sub_field('content_title');
      $text_colour = get_sub_field('text_colour');
      ?>
      <section id="<?= $content_title; ?>" class="<?= $text_colour; ?>" style="background-color:<?= $background_colour; ?>">
    	  <div class="container">
    	  	<?= $content_area; ?>
    	  </div>
      </section>
      <?} ?>
    <?} ?>

    Any help would be greatly appreciated.

    Thanks
    Jason

  • Jason, when you are retrieving a value from a checkbox, it’s returned an array of value, since the check box can have multiple choices.

    Following your code above, since checkbox can have multiples values, sometimes when the user select 2 checkboxes your code will not work.

    I recommend you to use the option Radio Button, then the user can select only one option only. If you still want to continue with the checkbox, you can do it by this way:

    <?php if( have_rows('checkbox_repeater') ): ?>
    
    	<?php while ( have_rows('checkbox_repeater') ) : the_row(); ?>
    	
    		<?php 
    			$color = get_sub_field('checkbox');
    			$background = get_sub_field('background');
    			$content = get_sub_field('content');
    			echo "color = "; var_dump($color); // just for debugging 
    		 ?>
    		<br />
    		<br />
    		<section style="background-color:<? echo $background[0]; ?>; color: <?php echo $color[0]; ?>; height:40px; width:100%;">
    			<?php echo $content; ?>
    		</section><br /><br /><br />
    	
    	<?php endwhile; ?>
    <?php endif; ?>

    The result of this code is on the file annexed.

    If you want to know more about the checkbox field, you can check the docs.
    Here is the link:
    [CHECKBOX] http://www.advancedcustomfields.com/resources/checkbox/

    To display the radio button, you don’t need to specify the array number, because for radio buttons it’s returned a normal string.

    $radio_field = get_sub_field('radio');
    echo $radio_field; // returned: #CCCCCC
    var_dump($radio_field); // debug, returned: string(4) "#ccc" 

    Have a nice day! =D

  • Thank you, Giovannicold I’ve changed them to radio buttons and that seemed to do the trick.

    Thanks
    Jason

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

The topic ‘Display a checkbox value within a repeatable field’ is closed to new replies.