项目: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
计数器对我的动态代码很重要。但我们可以用计数来计算总数。
对于计数,您可以像这样使用
<?php wp_count_posts( $type, $perm ); ?>
要获得已发布的状态类型,您需要调用wp_count_posts()函数,然后访问"publish"属性。
<?php
$count_posts = wp_count_posts();
$published_posts = $count_posts->publish;
?>
统计页面状态类型的方式与posts相同,并使用第一个参数。查找帖子状态的帖子数量的方法与查找帖子的方法相同。
<?php
$count_pages = wp_count_posts('page');
?>