
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.
if (!empty($color['value'])) {
// output label and value
}
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.