php 如何计算 ACF 中继器输出中的总行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43788167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the total number of rows in a ACF repeater output
提问by Robert
Question:How do you simply count the rows in the output of an ACF repeater field?
问题:如何简单地计算 ACF 转发器字段输出中的行数?
Goal:to make the output look different with a css class when there's only one row, vs. more than one row.
目标:当只有一行时,使用 css 类使输出看起来不同,而不是不止一行。
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.
显然,我在这里执行的方式将不起作用,因为第一次通过该行时计数 < 2,因此即使之后计算了更多行,它也会返回 true。
Thank you!
谢谢!
回答by Robert
OK, I finally found the answer to this.
好的,我终于找到了这个问题的答案。
The way to count total rows in an ACF repeater is:
在 ACF 中继器中计算总行数的方法是:
$numrows = count( get_sub_field( 'field_name' ) );
回答by csknk
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.
显然这用于get_the_ID()
检索当前的帖子 ID - 您可能需要修改它。
ACF stores the repeater count value against the repeater field name as the meta_key in the postmeta table.
ACF 将针对中继器字段名称的中继器计数值存储为 postmeta 表中的 meta_key。
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
.
ACF 使用计数来检索正确的转发器子字段值,这些值存储为针对 meta_keys 格式的值$repeaterFieldname . '_' . $index . '_' . $subfieldName
。
Hope this helps...
希望这可以帮助...
回答by Owais Alam
Working with the Repeater Field
使用中继器字段
repeater can be accessed by get_field or the_repeater_field / the_sub_field
可以通过 get_field 或 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
使用 ACF 值查询帖子
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.
查找事件(帖子类型)的示例,其中位置(自定义字段 - 选择)等于墨尔本(值)。这里有很多阅读: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>';
}
?>
回答by KennethRules
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>
回答by Jake
Advanced Custom Fields has a built in function to count the rows added in version 5.3.4
.
Advanced Custom Fields 有一个内置函数来计算 version 中添加的行5.3.4
。
Official Documenation: ACF | get_row_index()
You can use get_row_index();
inside the loop.
您可以get_row_index();
在循环内使用。
<?php if( have_rows('slides') ): ?>
<?php while( have_rows('slides') ): the_row(); ?>
<div class="accordion" id="accordion-<?php echo get_row_index(); ?>">
<h3><?php the_sub_field('title'); ?></h3>
<?php the_sub_field('text'); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
The index returned from this function begins at 1. This means a Repeater field with 3 rows of data will produce indexes of 1, 2 and 3.
此函数返回的索引从 1 开始。这意味着具有 3 行数据的 Repeater 字段将生成索引 1、2 和 3。
回答by Dhruv Saxena
Looks like you would want to reset the value in $numtestimonials
.
看起来您想重置$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;
回答by Anuruddh Pratap
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;?>