Wordpress loop - how to count items

后端 未结 3 1801
孤城傲影
孤城傲影 2021-01-07 19:56

Is there a way to get a number of items within Wordpress loop code:


This loop lists the p

相关标签:
3条回答
  • 2021-01-07 20:32

    I used this in mine

    <?php $count = 0;
      if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++;?>
            <div  class="col-lg-3">
                <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt();?></p>
            </div>
    
    <?php if ($count==4) { $count = 0;?>
            <div class="clearfix"></div>
    <?php } ?>
    
    <?php endwhile; endif; ?>
    
    0 讨论(0)
  • 2021-01-07 20:41

    You can use the post_count property of $WP_Query, like so:

    $wp_query->post_count
    

    Be aware of the difference with found_posts, which counts the posts which, though matching the query, are not being displayed (e.g. for pagination). You might want to use one or the other depending on your particular situation.

    0 讨论(0)
  • 2021-01-07 20:42

    Here's one way to go about it:

    <?php 
     $count = 0; //set up counter variable
     while (have_posts()) : the_post(); 
     $count++; //increment the variable by 1 each time the loop executes
     if ($count<4) {
        // here put the special code for first three
     }
     // here put the code for normal posts
     endwhile;
     ?>
    
    0 讨论(0)
提交回复
热议问题