我的脚本通过我的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;
为:-)
你的意思是在每次运行循环后存储$ i的值的数组? – Patashu 2013-03-25 23:32:06
是的。所以在每个循环结束时,我会在一个唯一的变量名称中有一个“$ i”总数。 – michaelmcgurk 2013-03-25 23:35:35
如果你发现自己在想像'我想存储任意数量的命名变量',你实际上需要一个具有数值数组的变量。阅读关于阵列,你会看到我的意思 – Patashu 2013-03-25 23:36:33