Home › Forums › Front-end Issues › Only Display data if True
Hi There, I am new to using this, so please bear with me. I have 3 fields that I only want displayed in a post if they are checked or equal to True. The fields are:
screened_porch
bed_linens_provided
fireplace
I have the post code as:
<?php echo get_field(‘screened_porch’); ?>
<?php echo get_field(‘bed_linens_provided’); ?>
<?php echo get_field(‘fireplace’); ?>
Right now if the check box is selected, a 1 shows on the post. I want it so that the field shows up i.e.: screened_porch or its label “Screened Porch” shows up. Any help is greatly appreciated. Thanks so much. -Beth
Hi Beth.
Well, if you validate it with a simple if, something like this:
<?php if ( get_field('fireplace' ) ) : ?>
// do something
<?php the_field('fireplace'); ?>
<?php endif; ?>
This will show the field only if the field “fireplace” have something, this works with text fields and any field as well.
Here the docs about it https://www.advancedcustomfields.com/resources/true-false/
Like a extra point. To print the value of a custom field, you can use directly the function the_field('your_field_name')
but if do you want to check or save the value of the field in a variable, you can use get_field('your_field_name')
Hi There, I am starting to understand this, but I am not quite there yet. I have a test post at:
https://www.barakaentodos.com/wp/test/
Where I checked:
screened_porch
bed_linens_provided
fireplace
and it displays out:
// do something 1 // do something 1 // do something 1
If the do something could be replaced with the field label or:
Screened Porch
Bed Linens Provided
Fireplace
that would be ideal, but I am not quite sure how to do that?
To display the label of the field you need to get the field object. There is an example of showing a field label on this page https://www.advancedcustomfields.com/resources/get_field_object/
The topic ‘Only Display data if True’ 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.