我的脚本循环浏览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;
非常感谢任何指示: - )
答案 0 :(得分: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
的密钥中。每次迭代后都不要忘记重置计数器!