dslkchyv673627
dslkchyv673627
2017-03-29 04:36

get_field()返回错误的数据

已采纳

I am using ACF repeater fields and I am trying to write a function which counts the number of rows of the repeater. The function returns 1 even if the number of rows in a repeater are 0 or more than 0. Can someone help me to fix this ?

$rows = get_field('sub_seminars');
$row_count = count($rows);
echo count($row_count);

A var_dump also returns int(1) even if the number of rows in a repeater are 0 or more than 0.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • du248227 du248227 4年前

    Let's have a look at your code:

    $rows = get_field('sub_seminars');
    $row_count = count($rows);  //returning some digit
    echo count($row_count);  //counting the returned digit
    

    You are double counting the returned count and that is why it is returning 1. Try this:

    $rows = get_field('sub_seminars');
    $row_count = count($rows);     //will return a digit
    echo $row_count;  //echo the count
    
    点赞 评论 复制链接分享