Home › Forums › Front-end Issues › 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);
}
The topic ‘Count single repeater field’ 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.