Home › Forums › General Issues › Display if value is not empty
Hi all,
I wrote this function that show the value of field next to label.
<?php
$color = get_field_object('color');
$finish = get_field_object('finish');
$material = get_field_object('met');
$warranty = get_field_object('warranty');
if( $color || $finish || $material || $warranty ): ?>
<div class="add-info add-info-l"> <?php echo $color['label']; ?></div><div class="add-info add-info-r"> <?php echo $color['value']; ?>
</div></br>
<div class="add-info add-info-l"> <?php echo $finish['label']; ?></div><div class="add-info add-info-r"> <?php echo $finish['value']; ?>
</div></br>
<div class="add-info add-info-l"> <?php echo $material['label']; ?></div><div class="add-info add-info-r"> <?php echo $material['value']; ?>
</div></br>
<div class="add-info add-info-l"> <?php echo $warranty['label']; ?></div><div class="add-info add-info-r"> <?php echo $warranty['value']; ?>
</div></br>
<?php endif; ?>
Can someone help me please what can I change / add to my code that if the field value is empty, the label of the specific field wont appear in front-end.
Thanks guy 🙂
I didn’t find any solution here.
Thank you! but if I want to check another field what should I do?
I tried
<?php elseif (!empty($finish['value'])): ?>
// Another field output
but it is not working.
thanks.
No “elseif”, you would check each field independently. I only gave you one example.
You can use this plugin https://wordpress.org/plugins/acf-views/ to easily display fields with a simple shortcode (like ‘[acf_views view-id=”x”]’), that can be pasted anywhere, including page editors. It has an option to display fields (and their label) only in case the field has a value.
You must be logged in to reply to this topic.
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.