Support

Account

Home Forums Front-end Issues Count single repeater field

Solving

Count single repeater field

  • Cheers,

    i got an error Message on count a single repeater field:
    Warning: count(): Parameter must be an array or an object that implements Countable

    Here is my code:

    <?php $count = count( the_sub_field('footer_coloumn_spalten') ); ?>
    <?php if(have_rows( 'footer_coloumn_spalten', 'option' )): $id = 0;  ?>
            <?php while ( have_rows( 'footer_coloumn_spalten', 'option' ) ) : the_row(); $id++;  ?>
                <?php if ( get_row_layout() == 'spalten_content' ) : ?>	
    
    <?php if ($count == "1"): ?>
       <div class="col-md-12 no-padding">
    <?php elseif ($count == "2"): ?>
        <div class="col-sm-12 col-md-6 no-padding">
    <?php elseif ($count == "3"): ?>
        <div class="col-xs-12 col-sm-6 col-md-4 no-padding">
    <?php else: ?>
        <div class="col-xxs-12 col-xs-6 col-sm-6 col-md-3 cards-break-sm no-padding">
    <?php endif; ?>
                
    <!-- Content -->
    <?php the_sub_field('footer_spalte'); ?>
    <?php echo $count; ?>
    <!-- /Content -->            
    
    <?php if ($count == "1"): ?>
        </div>
    <?php elseif ($count == "2"): ?>
        </div>
    <?php elseif ($count == "3"): ?>
        </div>
    <?php else: ?>
        </div>
    <?php endif; ?>
    
            <?php endif; ?>
    	<?php endwhile; ?>
    <?php else: ?>
    	<?php // no layouts found ?>
    <?php endif; ?>
    

    Count gives correct result, only the error message sucks my life….
    Have someone an idea ho to fix the error Message?

  • Bonjour,

    Plutôt que “the_sub_field()” ne faudrait-il pas utiliser “get_sub_field()” ?

  • Salut,
    je suis allemand mais essayez en français
    no get_sub_field conduit également à la même erreur.
    J’ai trouvé une solution sous-optimale, il doit y avoir un autre champ de répéteur devant le répéteur de comptage, alors ça marche.

  • the_sub_field() outputs the value, it does not return anything.

    Whether or not get_sub_field() causes an error it is still the function that you must use because you want to get the value and not display the value.

    get_sub_field() may return NULL or an empty value if the repeater has no rows. So you must get the field first and test it to see if it’s empty before performing any operation on the value

    
    $count = 0;
    $repeater_value = get_sub_field('footer_coloumn_spalten')) {
    if (!empty($repeater_value)) {
      $count = count($repeater_value);
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Count single repeater field’ is closed to new replies.