Wordpress - Count on different loop

2019-09-03 08:37发布

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

标签: php wordpress
3条回答
女痞
2楼-- · 2019-09-03 09:13

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

查看更多
够拽才男人
3楼-- · 2019-09-03 09:15
  <?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; ?> 
查看更多
倾城 Initia
4楼-- · 2019-09-03 09:21
<?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; 
?>

enter image description here

查看更多
登录 后发表回答