问题
Question: How do you simply count the rows in the output of an ACF repeater field?
Goal: to make the output look different with a css class when there's only one row, vs. more than one row.
My code:
if( have_rows('testimonials')) {
$counter = 0;
$numtestimonials = '';
//loop thru the rows
while ( have_rows('testimonials') ){
the_row();
$counter++;
if ($counter < 2) {
$numtestimonials = 'onlyone';
}
echo '<div class="testimonial ' . $numtestimonials . '">';
// bunch of output here
echo '</div>';
}
}
Obviously, The way I do it here will not work as the count is < 2 the first time thru the row, so it returns true even if more rows are counted after.
Thank you!
回答1:
OK, I finally found the answer to this.
The way to count total rows in an ACF repeater is:
$numrows = count( get_sub_field( 'field_name' ) );
回答2:
You can get the row count like this:
$count = get_post_meta(get_the_ID(), 'testimonials', true);
Obviously this uses get_the_ID()
to retrieve the current post ID - you may need to amend this.
ACF stores the repeater count value against the repeater field name as the meta_key in the postmeta table.
ACF uses the count to retrieve the correct repeater subfield values, which are stored as values against meta_keys with the format $repeaterFieldname . '_' . $index . '_' . $subfieldName
.
Hope this helps...
回答3:
Looks like you would want to reset the value in $numtestimonials
.
So, effectively, the code with a fix would be:
$output = "";
$numtestimonials = "";
while ( have_rows('testimonials') ){
the_row();
$counter++;
$output .= "<span>" .$some_data. "</span>"; // bunch of output;
}
if($counter < 2){
$numtestimonials = "onlyone";
}
$output = "<div class='testimonail ".$numtestimonials." '>"
.$output
."</div>";
echo $output;
回答4:
Working with the Repeater Field
repeater can be accessed by get_field or the_repeater_field / the_sub_field
<?php if( have_rows('repeater_field_name') ): ?>
<ul>
<?php while( have_rows('repeater_field_name') ): the_row(); ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Randomly select a repeater field row
<?php
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);
echo $rows[ $i ]['sub_field_name'];
?>
Getting values from another page
<?php
$other_page = 12;
?>
<p><?php the_field('field_name', $other_page); ?></p>
<?php
// get variable
$variable = get_field('field_name', $other_page);
// repeater field: note that the_sub_field and get_sub_field don't need a post id parameter
if( have_rows('repeater_field_name', $other_page) ): ?>
<ul>
<?php while( have_rows('repeater_field_name', $other_page) ): the_row(); ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Query posts with ACF values
An example of finding Events (post type) where location (custom field – select) equals Melbourne (value). Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'melbourne'
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
}
echo '</ul>';
}
?>
回答5:
You may wanted to try this..
<?php
$row = get_field('repeater', $post->ID);
if($row < 1) {
$rows = 0;
} else {
$rows = count($row);
} ?>
<p>Number of Row is (<?php echo $rows ; ?>)</p>
回答6:
You can try this code its working fine :
<?php if( have_rows('repeater_name') ):
$my_fields = get_field_object('repeater_name');
$count = (count($my_fields));
echo $count;
endif;?>
来源:https://stackoverflow.com/questions/43788167/how-to-count-the-total-number-of-rows-in-a-acf-repeater-output