questions

Loop to display all fields/rows from ACF group

Rate this post

I want to display all rows from one of ACF group using the code below. Unfortunately nothing shows up. Could I ask for help which is incorrect?

<div class="product-atributes">
    
        <div class="atributes-form">
        <?php 
        for ($i=1; $i<3; $i++) {
            if (have_rows($atrybut.$i)) {
                while (have_rows('atrybut_'.$i)) {
                    the_row();
        // vars
        $quantity = get_sub_field('quantity');
        $ean = get_sub_field('ean');
        $linkap = get_sub_field('linkap');
        ?>
            <div class="flex-body">
                <div class="flex-column">
                    <div>Quantity: <?php echo $quantity ?></div>
                    <div>EAN: <?php echo $ean ?></div>
                </div>
                <div class="flex-row">
                    <div><span><a href="<?php echo $linkap ?>" target="_blank" rel="nofollow">BUY</a></span></div>
                </div>
            </div>
            <?php } } } 
            ?>
        </div>
    </div>

 

✔️Solution:

Looks like you have a typo here:

  if (have_rows($atrybut.$i)) {
                while (have_rows('atrybut_'.$i)) {

Which is the correct field name $atrybut.$i or 'atrybut_'.$i?

You are also missing some ending ;

UPDATE: Adding conditional display:

...

$quantity = get_sub_field('quantity');
$ean = get_sub_field('ean');
$linkap = get_sub_field('linkap');

// may need to tweak this logic based on what the
// values might/could be but this should get you going
$has_all_acf_fields = $quantity && $ean && $linkap;

if ($has_all_acf_fields) {
?>
    <div class="flex-body">
        <div class="flex-column">
            <div>Quantity: <?php echo $quantity; ?></div>
            <div>EAN: <?php echo $ean; ?></div>
        </div>
        <div class="flex-row">
            <div><span><a href="<?php echo $linkap; ?>" target="_blank" rel="nofollow">BUY</a></span></div>
        </div>
    </div>
<?php 
}

...

Leave a Reply

Your email address will not be published.

Back to top button