No matter what I try I cannot get true/false field to evaluate true…I would like to add a “target=_blank” attribute to an element when the box is checked in the edit screen. Below is the code I have, I have already tried about 50 permutations and never get a true value.
<?php if(get_field('video_box')): ?>
<ul class="video_box">
<?php while(has_sub_field('video_box')): ?>
<li>
<a <?php if(get_field('open_where')) { ?>target="_blank" <? } else { } ?> href="<?php the_sub_field('video_thumbnail_link'); ?>"><img src="<?php the_sub_field('video_thumbnail_image'); ?>" alt="Kathy Cramer Video" /></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hi @john.marcello
a boolean function return true or false. so a true / false checkbox return true or false too.
this code should resolve your problem:
target="<?php echo get_field( 'open_where', false, false ) == true ? '_blank' : '_self'; ?>"
so your <a>
link’s code should looks like this:
<a target="<?php echo get_field( 'open_where', false, false ) == true ? '_blank' : '_self'; ?>" href="<?php the_sub_field('video_thumbnail_link'); ?>">
hope this help you
A
Hi @john.marcello
I suspect the issue is that you are using teh get_field function within a repeater loop, when you shoudl be using get_sub_field instead!
Could that explain the issue?
@astrixoblix – the get_field function will return a true / false without the 2 ‘false’ parameters. In fact, the value saved into the DB is either ‘1’ or ‘0’, so it is best to loosly compare like so:
if( get_sub_field('field_name') ):
Thanks
E
Thanks I will play with this some more today.