Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got three loops set up via jQuery UI tabs as seen in my code below. All works good but i'd like to be able to display the "count" of each loop in the tab label. I obviously can't just echo it because it's before the loop is even called and outside of it.

Any suggestions? Maybe call the loops before and put each one into a variable or something? Not even sure if that can be done... Thanks

        <div id="tabs">
            <ul>
                <li><a href="#loop1"><span>loop 1 (loopcount)</span></a></li>
                <li><a href="#loop2"><span>loop 2 (loopcount)</span></a></li>
                <li><a href="#loop3"><span>loop 3 (loopcount)</span></a></li>
            </ul>
            <section id="loop1"> 
                <?php include (TEMPLATEPATH . '/include/loop1.php'); ?> 
            </section>
            <section id="loop2" class="ui-tabs-hide"> 
                <?php include (TEMPLATEPATH . '/include/loop2.php'); ?> 
            </section>
            <section id="loop3" class="ui-tabs-hide"> 
                <?php include (TEMPLATEPATH . '/include/loop3.php'); ?>  
            </section>
        </div>
share|improve this question
    
What are you trying to count? The amount of posts in each loop? –  Jared Jan 20 '12 at 13:36
    
Yes, sorry the loop "count" –  Blackbird Jan 20 '12 at 13:45
add comment

1 Answer

up vote 1 down vote accepted

You would have to perform the query for each respective tab. If you are using WP_Query then you can perform the query and then use $found_posts to return the total number of posts matching that query. For instance,

$query_tab_1 = new WP_Query($args); //Where $args is some arguments for your query
$tab_1_count = $query_tab_1->found_posts;

The $query_tab_1 object will have all the returned posts, so inside the appropriate 'section' you could just loop through that:

    if($query_tab_1->have_posts()):
      while ($query_tab_1->have_posts() ) : $query_tab_1->the_post();
        //Display tab 1's loop here
      endwhile;
    endif;

See the Codex on the WP_Query object. If you aren't using the WordPress API, then you could use a MYSQL COUNT query to return the number of hits for each query..

Dislaimer: I haven't tested this - but it should work.


EDIT

There appears to be some people experiencing issues with this method - it seems to a php bug (but some have managed to fix it, see link). As alternative (and assuming you don't want pagination - you could try get_posts which returns an array of posts, and then use php's count. It's not as elegant but hopefully the related link will help solve the issue...

share|improve this answer
    
Thanks, the loop works using your method but i don't understand the found_posts part. I tried to simply echo $tab_1_count in the Tab label area but it just displays 0 –  Blackbird Jan 20 '12 at 13:58
    
found_posts is a variable of the particular instance of your WP_Query object (in this case $query_tab_1). It should return the number of posts. After doing some research, some people have been experiencing issues. I shall update my answer... –  Stephen Harris Jan 20 '12 at 14:20
    
Thanks again, the get_posts method works but it means i'll have to double up on the queries. Is this the only way around this? :/ –  Blackbird Jan 20 '12 at 15:07
    
If pagination isn't an issue you could use a foreach loop to go through the array... Did you see the link in my answer? - it seems some people have found a way round the bug. –  Stephen Harris Jan 21 '12 at 12:36
    
I did, trouble is i'm on shared hosting and have no access to apache. –  Blackbird Jan 23 '12 at 16:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.