2

我的脚本循环浏览我的 WordPress 网站上的帖子。

我还在计算未发布帖子的数量并将其存储在我的$i变量中。

但是,由于这是循环多个结果,因此我需要$i在每次迭代后将最终数字存储在一个唯一变量中。

我该如何处理?我可以$currentID在任何阶段使用我的变量吗?

这是我的PHP片段供参考:

while ( $query->have_posts() ) : $query->the_post();

    $currentID = get_the_ID();

        while ( $events_list->have_posts() ) :

            $events_list->the_post();

            $postdate = get_the_date('Y-m-d');
            $currentdate = date('Y-m-d');

            if ($postdate > $currentdate) {
                $i++;
            }

        endwhile;

// preferrably store the total of $i here in a unique variable then reset $i

    the_content();

endwhile;

非常感谢任何指针:-)

4

1 回答 1

1

为什么不让一个数组通过一个键来保存所有值$currentID

$postCount = array();
while(condition){
  $currentID = get_the_ID();
  $i = 0; // don't forget to reset your counter
  // processing goes here
  while(condition){
    // more processing goes here
    $i++;
  }
  $postCount[$currentID] = $i;
}

这将为您留下一个数组,其中包含$i外部循环的每次迭代的值。的值$i将存储在等于 的键中$currentID。不要忘记在每次迭代后重置您的计数器!

于 2013-03-25T23:33:37.760 回答