Wordpress - Count on different loop

2019-09-03 08:58发布

问题:

How should I put count on this loop?

<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
while ($loop->have_posts()) :  $loop->the_post();  ?>

Someone can help me?

Thanks so much

Complete code:

<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
while ($loop->have_posts()) :  $loop->the_post();  ?>

<article class="article one-third column">

<div class="thumbnail">
<?php the_post_thumbnail('latest-news-thumb'); ?>
</div>

<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?><span>.</span></a></h2>

<div class="meta">
<span><?php _e('Postado em -', 'kula'); ?> <?php the_category(' & '); ?><br />on <strong><?php the_time('F jS, Y'); ?></strong></span>
<span><i class="icon-comment"></i> <a href="<?php the_permalink(); ?>#comments"><?php $commentscount = get_comments_number(); echo $commentscount; ?> <?php _e('Comentários', 'kula'); ?></a></span>
</div>

<?php the_excerpt(); ?>

<a class="read-more-btn" href="<?php the_permalink() ?>"><?php _e('Leia mais', 'kula'); ?> <span>&rarr;</span></a>

</article><!-- end article -->

<?php endwhile; ?>

I need count 3 by 3 posts and put a class on article...

So i can put this code on article class:

<?php if (($count%3)==0) {echo ' last';}?>

Thanks

回答1:

  <?php

    global $data;
    $args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
    $loop = new WP_Query($args);
    $totalPost = count($loop->posts); //will give total number of posts

 ?>

Edit:

This will insert last class in article after 3 posts

    <?php
    global $data;
    $args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
    $loop = new WP_Query($args);
    $postNo=0;
    while ($loop->have_posts()) :  $loop->the_post();  ?>

    <article class="article one-third column <?php echo (($postNo++)%3==0)?' last ':'' ;?>">

    <div class="thumbnail">
    <?php the_post_thumbnail('latest-news-thumb'); ?>
    </div>

    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?><span>.</span></a></h2>

    <div class="meta">
    <span><?php _e('Postado em -', 'kula'); ?> <?php the_category(' & '); ?><br />on <strong><?php the_time('F jS, Y'); ?></strong></span>
    <span><i class="icon-comment"></i> <a href="<?php the_permalink(); ?>#comments"><?php $commentscount = get_comments_number(); echo $commentscount; ?> <?php _e('Comentários', 'kula'); ?></a></span>
    </div>

    <?php the_excerpt(); ?>

    <a class="read-more-btn" href="<?php the_permalink() ?>"><?php _e('Leia mais', 'kula'); ?> <span>&rarr;</span></a>

    </article><!-- end article -->
    <?php endwhile; ?> 


回答2:

After $loop = new WP_Query($args); use Sanjeev's suggestion i.e, $totalPost = count($loop->posts);



回答3:

<?php
    global $data;
        $args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
        $loop = new WP_Query($args);
        $count = 1; // add count variable
            while ($loop->have_posts()) :  $loop->the_post();  ?>

            <article class="article one-third column<?php if (($count%3)==0) {echo ' last';}?>">

              <!-- put your code here -->


            </article>
<?php 
        $count++; //count ++
        endwhile; 
?>



标签: php wordpress