Home › Forums › General Issues › If True do this / False do that
What I would like to achieve is if True is ticked, display X content. Otherwise, display Y content. And currently, I’ve gotten up to this point:
<?php if ( get_field('true_false_box') == true ) { ?>
Display X content
<?php } else { ?>
Display Y content
<?php } ?>
Problem I’m facing now is both tick / untick the True/False checkbox displays only the Y content. However, the var_dump reports the true / false status correctly.
I don’t necessarily see any errors in that code but you don’t have to do == true
. Make sure you have the right field name and you are testing on a post/page that has content in there.
You can also improve/clean up your code a bit. The code below is very readable but be careful not to use it too much or you wont easily see which endif
belongs to which if
.
<?php if ( get_field( 'field_name' ) ): ?>
This is displayed when the field_name is TRUE or has a value.
<?php else: // field_name returned false ?>
This is displayed when the field is FALSE, NULL or the field does not exist.
<?php endif; // end of if field_name logic ?>
I used your code, Tim and am getting this PHP error:
Error while parsing PHP: syntax error, unexpected 'else' (T_ELSE)
my code:
<?php if ( get_field( 'btn_show' ) ); ?>
<p class="text-center"><a class="btn" href="<?php the_sub_field('btn_url'); ?>"><?php the_sub_field('btn_text'); ?></a></p>
<?php else: // field_name returned false ?>
<?php endif; // field_name ?>
Hey tigre. Your if ( get_field( 'btn_show' ) );
should be if ( get_field( 'btn_show' ) ):
.
The semicolon (;
) ended the line so that’s why your else
was “unexpected” according to PHP.
Hi, I don’t seem to be able to get this working using the following:
<?php if ( get_field( 'featured' ) ): ?>
<div class="featured">
<?php else: ?>
<div class="not-featured">
<?php endif; ?>
<div class="not-featured">
Is outputted if the featured True/False field is checked or not.
Any ideas what I’m doing wrong?
Cheers.
You could start by dumping the raw data that get_field gets for you and see if that matches your expectations in various scenerio’s (checked, unchecked, field not existing).
I use a snippet like this to dump things (because I don’t like how var_dump() looks):
echo "<pre style=\"text-align:left;padding:10px;border:1px solid red;background-color:#fff;color:#111;font-family:Courier;\">";
print_r( get_field( 'featured' ) );
echo "</pre>";
I used this withe the true/false Field Type :
$true = get_post_meta( get_the_ID(), 'true_false', true );
if ( $true == '1' ) {
// Do Something
}
Try this code. Make sure to add the #hide_this_div to the whatever you want to hide:
add_action('template_redirect', 'hide_disabled_blocks');
function hide_disabled_blocks() {
global $post;
// get dynamic project id
$post_id = $post->ID;
// get value of true-false field in each post
$hidediv = get_post_meta($post_id, 'ACF-field-name', true);
if is_singular( 'post' ) :?>
<style type="text/css">
<?php
// if "ACF true-false command" is false, hide the Div ID
if ( ($hidediv == '0') || (empty($hidediv)) ): ?>
#hide_this_div { display: none; }
<?php endif;
?>
</style>
The topic ‘If True do this / False do that’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.