Wordpress Loop - How to Count Elements

Is there a way to get multiple elements in a Wordpress loop code:

<?php while (have_posts()) : the_post(); ?> 

This loop lists the messages. I need to add certain classes to the first 3 depending on their total number.

+13
source share
4 answers

You can use the post_count property of $WP_Query , for example:

 $wp_query->post_count 

Pay attention to the difference with found_posts , which counts messages that, although they correspond to the query, are not displayed (for example, for pagination). You might want to use one or another option depending on the specific situation.

+23
source

Here is one way:

 <?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; ?> 
+13
source

I used it in my

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

Online Quran Academy Online Quran Academy : Your families are important, your journey into learning the Arabic language and {Quran} can begin with three simple steps.

(LQC) facilitates your goal, every impression of the path to achieving excellent, online learning of the Quran.

and teaching Arabic. With the LQC Online Quran Academy and Online Arabic Language Academy, you can read at home.

0
source

Source: https://habr.com/ru/post/955704/


All Articles