项目: WordPress的项目
查询:WP_Query()
计数总项目 - 循环中循环
随着我负责的两个循环的单号查询 - 我把它称为一个循环内循环。结构如下所示:
<?php
while($my_query -> have_posts()) :
$my_query -> the_post();
if(condition) { ?>
<div class="grid-box">
<div class="item">Item of this kind</div>
</div> <!-- .grid-box -->
<?php
}
if(condition) {
$someCounter = grab some counter here;
for($inner = 0; $inner < $someCounter; $inner ++) {
?>
<div class="grid-box">
<div class="item">Item of that** kind#<?php echo $inner; ?></div>
</div> <!-- .grid-box -->
<?php
} //end for
}
endwhile;
?>
对于CSS查询对我来说是非常出色的工作,显示了漂亮的网格块中的项目。但是如果物品多于一排,则第二排中的物品会与第一排相冲突。所以我打算把它们排级内等:
<div class="row">
<!-- every 6 items within a grid -->
<div class="grid grid-first>Item of this kind</div>
<div class="grid>Item of this kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
<div class="grid grid-last>Item of that** kind</div>
</div>
<div class="row">
<div class="grid grid-first>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
</div>
现在我需要计算总的项目。我怎样才能做到这一点?我是否需要传递两个计数器,如果是这样,那么我怎样才能将它们结合起来计算确切的计数,然后使用计数作为条件来加载与.row
DIV?请注意,正如我正在处理的那样,$inner
计数器对于我的动态代码很重要。但是我们可以使用总数来计数。
开始第一while循环之前,初始化'$ intTotal = 0;',增量'$ intTotal ++;'内的for循环和输出'打印的sprintf( '%d总量行',$ intTotal) ;'在你的后面。 – SaschaM78