1

I can't figure out how in the world to get the value of a checkbox entered in a repeater field. There is only one choice. Basically if the user ticks the checkbox it should output a class.

In the code example I don't have the value in the class yet because I am just trying to get it to print something! arg.

Thanks for any and all help, I appreciate it!

<?php if( have_rows( 'milestone_module' ) ) : ?>

<section id="tao-timeline">

    <?php 

        while( have_rows( 'milestone_module' ) ) : the_row(); 
        // Vars
        $milestone_module_date = get_sub_field( 'milestone_module_date' );
        $milestone_module_copy = get_sub_field( 'milestone_module_copy' );
        $field = get_field_object('milestone_long_date');
        $value = get_field('milestone_long_date');
        $label = $field['choices'][ $value ];
    ?>

    <div class="tao-timeline-block">
        <?php if( $milestone_module_date ) : ?>
            <span class="tao-timeline-date">
                <?php echo $label; ?>
                <?php echo $milestone_module_date; ?>
            </span><!--/.tao-timeline-date-->
        <?php endif; ?>

        <?php if( $milestone_module_copy ) : ?>
            <div class="tao-timeline-content">
                <?php echo $milestone_module_copy; ?>
            </div><!--/.tao-timeline-content-->
        <?php endif; ?>

    </div><!--/.tao-timeline-block-->

    <?php endwhile; wp_reset_postdata(); ?>

</section><!--/#tao-timeline-->

<?php endif; ?>

Updated code for true/false, still not working...

<?php if( have_rows( 'milestone_module' ) ) : ?>

                        <section id="tao-timeline">

                            <?php 

                                while( have_rows( 'milestone_module' ) ) : the_row(); 
                                // Vars
                                $milestone_module_date = get_sub_field( 'milestone_module_date' );
                                $milestone_module_copy = get_sub_field( 'milestone_module_copy' );
                            ?>

                            <div class="tao-timeline-block">
                                <?php if( get_field('milestone_long_date') ) { ?>
                                    <span class="tao-timeline-date long-date">
                                        <?php echo $milestone_module_date; ?>
                                    </span><!--/.tao-timeline-date-->
                                <?php } else { ?>
                                    <span class="tao-timeline-date">
                                        <?php echo $milestone_module_date; ?>
                                    </span><!--/.tao-timeline-date-->
                                <?php } ?>

                                <?php if( $milestone_module_copy ) : ?>
                                    <div class="tao-timeline-content">
                                        <?php echo $milestone_module_copy; ?>
                                    </div><!--/.tao-timeline-content-->
                                <?php endif; ?>

                            </div><!--/.tao-timeline-block-->

                            <?php endwhile; wp_reset_postdata(); ?>

                        </section><!--/#tao-timeline-->

                    <?php endif; ?>
Chris Lee
  • 13
  • 5
  • Is the class in the value, or it is one set value if they check the box? – Aibrean Sep 09 '14 at 11:49
  • one set value if the check the box. If the box isn't checked, no class gets printed. The class would be .long-date, if they don't check the box, that class is not added. – Chris Lee Sep 09 '14 at 17:29
  • So you mean it's a true/false field? See http://www.advancedcustomfields.com/resources/true-false/ – Aibrean Sep 09 '14 at 18:00
  • Yes, it is a true/false. Perhaps I have it set up wrong? – Chris Lee Sep 09 '14 at 18:20
  • Yep...your stuff is coded like a checkbox (where it's getting an option). True/false is different. It behaves like an "if" statement. Which field is true/false and I might be able to help you restructure. – Aibrean Sep 09 '14 at 18:49
  • I changed it to a true false and it's still not working :/. I updated the code above to show the new code. – Chris Lee Sep 09 '14 at 20:57
  • what FIELD is true/false? – Aibrean Sep 09 '14 at 21:01
  • milestone_module is the repeater and the true/false is milestone_long_date – Chris Lee Sep 09 '14 at 21:31

1 Answers1

1

Chris, I'm fairly sure that this is how you would get this to work (for just the true/false segment):

  <?php if (get_sub_field('milestone_long_date') == true) { ?>
      <span class="tao-timeline-date long-date">
      <?php echo $milestone_module_date; ?>
      </span><!--/.tao-timeline-date-->
  <?php } else { ?>
       <span class="tao-timeline-date">
       <?php echo $milestone_module_date; ?>
       </span><!--/.tao-timeline-date-->
  <?php } ?>

Otherwise, if you had a radio field instead, I know the code for that. I've not had to work with true/false.

If it was a yes/no radio button:

<?php $long-date = get_sub_field('milestone_long_date');  ?>

 <?php if ($long-date == 'Yes') { ?>
           <span class="tao-timeline-date long-date">
          <?php echo $milestone_module_date; ?>
          </span><!--/.tao-timeline-date-->
<?php } else { ( $long-date == 'No' ) ?>
         <span class="tao-timeline-date">
        <?php echo $milestone_module_date; ?>
         </span><!--/.tao-timeline-date-->
<?php }; ?>

I know the radio will work. I'd have to do some testing on the true/false to confirm.

Aibrean
  • 6,297
  • 1
  • 22
  • 38
  • The true/false still isn't working, I'll opt for the radio button direction. It isn't going to make much difference to the user either way. I'll report back how it goes. Thanks for the help Aibrean! – Chris Lee Sep 10 '14 at 04:03
  • For true/false I forgot...you're in a repeater, it needs to be get_sub_field! Your code in your post might work if you use get_sub_field for the if/else. – Aibrean Sep 10 '14 at 11:35
  • get_sub_field worked perfectly! Awesome, thanks Aibrean! I really appreciate your help. – Chris Lee Sep 10 '14 at 17:53
  • It came to me right when I woke up. I had a Ben Franklin moment :) – Aibrean Sep 10 '14 at 17:54