Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算两个循环中的项目总数-循环中的循环_Php_Wordpress_Loops_Counter - Fatal编程技术网

Php 计算两个循环中的项目总数-循环中的循环

Php 计算两个循环中的项目总数-循环中的循环,php,wordpress,loops,counter,Php,Wordpress,Loops,Counter,项目:WordPress项目 查询: 对于单个查询,我处理两个循环——我称之为循环中的循环。结构如下: <?php while( $my_query -> have_posts() ) : $my_query -> the_post(); if( condition) { ?> <div class="grid-box"> <div class="item">Item of this kind</div>

项目:WordPress项目
查询:

对于单个查询,我处理两个循环——我称之为循环中的循环。结构如下:

<?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,查询对我来说做得非常好,在漂亮的网格块中显示项目。但如果项目多于一行,则第二行中的项目与第一行中的项目发生冲突。因此,我计划将它们放在row类中,如:

<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>

这类物品
同类物品
同类物品
同类物品

对于计数,您可以这样使用

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

要获取发布的状态类型,可以调用wp_count_posts()函数,然后访问“publish”属性

<?php
$count_posts = wp_count_posts();

$published_posts = $count_posts->publish;
?>

计数页面状态类型的方式与POST相同,并使用第一个参数。查找post状态的post数量的方法与post相同

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


在启动第一个while循环之前,初始化
$intTotal=0,增量
$intTotal++在for循环和输出中
打印sprintf(“%d行总数”,$intTotal)在您的endwhile后面。很抱歉,
wp\u count\u posts()
不适合这里,因为它只计算主循环的posts,而不计算内部循环(
for loop
)项。