2013-11-27 151 views
0

项目: 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计数器对于我的动态代码很重要。但是我们可以使用总数来计数。

+0

开始第一while循环之前,初始化'$ intTotal = 0;',增量'$ intTotal ++;'内的for循环和输出'打印的sprintf( '%d总量行',$ intTotal) ;'在你的后面。 – SaschaM78

回答

0

对于数,您可以使用这样

<?php wp_count_posts($type, $perm); ?> 

要获得发布的状态类型,你会调用wp_count_posts()函数,然后访问“发布”属性。

<?php 
$count_posts = wp_count_posts(); 

$published_posts = $count_posts->publish; 
?> 

计数页面状态类型以相同的方式完成的职位和利用的第一个参数。查找帖子状态的帖子数量与帖子相同。

<?php 
$count_pages = wp_count_posts('page'); 
?> 
+0

对不起,'wp_count_posts()'在这里不适合,因为它只会计算主循环的帖子 - 而不是内循环('for loop')项目。 –