Home › Forums › Add-ons › Repeater Field › Hide subfield if it has no text
I’m using ACF with the Repeater Field Add-On to create price lists on a site. I’ve used the “basic PHP approach” described in the Repeater documentation (http://www.advancedcustomfields.com/resources/field-types/repeater/) to create my price lists. This works well on items that have all three of the sub-fields (product, price, description) (see http://lulusalonsf.com/2013/skin/–it does still need CSS work). But when the products don’t have a description field, the code still displays an empty paragraph for the description (see the extra spacing between items on http://lulusalonsf.com/2013/hair/). I tried using the “get_sub_field within the has_sub_field loop” described in the Repeater documentation but it didn’t work (my PHP knowledge is still new, so this may be the wrong approach altogether.)
Is there a way to fetch and display the description sub-field only when it is not empty?
The “basic PHP approach” I’m using is as follows:
<div class="pricelist"><?php $rows = get_field('price_list');
if($rows)
{
echo '<ul class="leaders">';
foreach($rows as $row)
{
echo '<li><span class="service">' . $row['product_or_service'] . '</span> <span class="price"> ' . $row['price'] .'</span></li>';
echo '<p class="description"> ' . $row['description'] .'</p>';
}
echo '</ul>';
} ?>
</div>
Thanks in advance for your help.
jeannine
Just check if the field is populated with PHP:
<div class="pricelist"><?php $rows = get_field('price_list');
if($rows)
{
echo '<ul class="leaders">';
foreach($rows as $row)
{
echo '<li><span class="service">' . $row['product_or_service'] . '</span> <span class="price"> ' . $row['price'] .'</span></li>';
if( $row['description']) echo '<p class="description"> ' . $row['description'] .'</p>';
}
echo '</ul>';
} ?>
</div>
How do I check if the field is populated with PHP? I must be missing something–I don’t see any difference between the code I posted and the code in your response.
You’ll need to do a check for $row['description']
before actually echoing the <p>
tag – that’s why I placed if( $row['description'])
before echo '<p class="description">...
. This check will simply make sure that $row['description']
is not empty.
If this doesn’t work for you, PHP can also check for empty values in a number of different ways:
http://php.net/manual/en/function.empty.php
http://php.net/manual/en/function.is-null.php
And there’s a discussion of some implementations over at http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/ !
Ah, and just for ease of reading: http://diffchecker.com/7za0xnhq 🙂
Thank you so much…I knew I was missing something. Adding the magic if( $row[‘description’]) works beautifully.
jeannine
The topic ‘Hide subfield if it has no text’ 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.