0

Project: WordPress project
Query: WP_Query()

With the single query I'm dealing with two loops - I call it loop within a loop. Structure is like below:

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

With CSS the query is doing excellent job for me, showing the items in nice grid-blocks. But with more items than a row, the items in second row colliding with the first. So I planned to put them within row class like:

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

Now I need to count the total items. How can I do this? Do I need to pass two counter and if so then how can I combine them both to count the exact counts and then use the count as conditions to load the div with .row? Please note as what I'm dealing with, the $inner counter is important for my dynamic code. But we can use the count for our total count.

1
  • Before starting the first while-loop, initialize $intTotal=0;, increment $intTotal++; inside your for-loop and output print sprintf('%d total rows', $intTotal); behind your endwhile. Commented Nov 27, 2013 at 11:00

1 Answer 1

0

For count you can use like this

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

To get the published status type, you would call the wp_count_posts() function and then access the 'publish' property.

<?php
$count_posts = wp_count_posts();

$published_posts = $count_posts->publish;
?>

Counting pages status types are done in the same way as posts and make use of the first parameter. Finding the number of posts for the post status is done the same way as for posts.

<?php
$count_pages = wp_count_posts('page');
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, wp_count_posts() won't fit here, because it'll count only the main loop's posts - not the inner loop (for loop) items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.