Home › Forums › General Issues › hide div if nothing in fields
Hi, is there a way to hide my container if one of these fields is missing?
<div class="container">
<div class="row">
<div class="col">
<?php if( get_field('preco') ): ?>
<p> <?php the_field('preco'); ?> </p>
<?php endif; ?>
</div>
<div class="col">
<?php
$ratingstar = get_field('checked');
?>
<span class="fa fa-star <?php if ($ratingstar >= 1) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 2) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 3) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 4) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar == 5) { echo 'checked';}?>"></span>
</div>
</div>
</div>
Not sure why my original answer got marked as private but here’s what I originally provided as solution:
<?php if( get_field('preco') && get_field('checked')): ?>
<div class="container">
<div class="row">
<div class="col">
<?php if( get_field('preco') ): ?>
<p> <?php the_field('preco'); ?> </p>
<?php endif; ?>
</div>
<div class="col">
<?php $ratingstar = get_field('checked'); ?>
<span class="fa fa-star <?php if ($ratingstar >= 1) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 2) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 3) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar >= 4) { echo 'checked';}?>"></span>
<span class="fa fa-star <?php if ($ratingstar == 5) { echo 'checked';}?>"></span>
</div>
</div>
</div>
<?php endif; ?>
The “if” condition in the first line (together with “endif” in the last line) hide the content if there is no 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.