如何计算ACF转发器输出中的总行数

时间:2017-05-04 16:21:00

标签: php mysql wordpress repeater advanced-custom-fields

问题:如何简单计算ACF转发器字段输出中的行数?

目标:,当只有一行而不是一行时,使输出看起来与css类不同。

我的代码:

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>';                          
    }
}

显然,我在这里的方式不会起作用,因为计数是&lt; 2第一次通过该行,因此即使在计数后计算更多行,它也会返回true。

谢谢!

9 个答案:

答案 0 :(得分:17)

好的,我终于找到了答案。

计算ACF转发器中总行数的方法是:

$numrows = count( get_sub_field( 'field_name' ) );

答案 1 :(得分:0)

您希望重置$numtestimonials中的值。

因此,有效地,带有修复的代码将是:

$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;

答案 2 :(得分:0)

您可以像这样获取行数:

$count = get_post_meta(get_the_ID(), 'testimonials', true);

显然,这会使用get_the_ID()来检索当前的帖子ID - 您可能需要修改此内容。

ACF将转发器计数值与转发器字段名称一起存储为postmeta表中的meta_key。

ACF使用计数来检索正确的转发器子字段值,这些值作为值存储为格式为$repeaterFieldname . '_' . $index . '_' . $subfieldName的meta_keys。

希望这会有所帮助......

答案 3 :(得分:0)

使用“转发器字段”

可以通过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; ?>

随机选择转发器字段行

<?php

$rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);

echo $rows[ $i ]['sub_field_name'];

?>

从其他网页获取值

<?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; ?>

使用ACF值查询帖子

查找事件(帖子类型)的示例,其中location(自定义字段 - 选择)等于墨尔本(值)。 有很多东西可以在这里阅读: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>';
}

?>

答案 4 :(得分:0)

你可能想试试这个..

<?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>

答案 5 :(得分:0)

“高级自定义字段”具有内置功能,可以对版本5.3.4中添加的行进行计数。

官方文档:ACF | get_row_index()

您可以在循环内使用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; ?>

此函数返回的索引从1开始。这意味着具有3行数据的Repeater字段将产生1、2和3的索引。

答案 6 :(得分:0)

这对我有用,计数必须放在 if(have_rows('repeater_field') :

如果中继器为空,三元运算符可避免警告错误

如果你把 count 放在 "if(have_rows('repeater_field')) :" 之后,count 返回 FALSE

$repeater_field = get_sub_field('repeater_field');
// OR if repeater isn't a sub_field
// $repeater_field = get_field('repeater_field');

// ternary operator to avoid warning errors if no result
$count =  $repeater_field ? count($repeater_field) : FALSE;

if(have_rows('repeater_field')) : // OR if($count) :

    echo 'Number of posts:' . $count . '<br>';

    while(have_rows('repeater_field')) : the_row();
        echo get_sub_field('field_name') . '<br>';
    endwhile;
endif;

答案 7 :(得分:-1)

您可以尝试使用此代码:

<?php if( have_rows('repeater_name') ):
$my_fields = get_field_object('repeater_name');
$count = (count($my_fields));
echo $count;
endif;?>

答案 8 :(得分:-1)

ACF 中继器字段使用数组来存储行 获取字段行数的最简单方法是

$count = sizeof(get_sub_field('field_name'));
相关问题