0

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

4
  • Where is count, show full script. Commented Mar 27, 2014 at 6:05
  • I need put count on this loop, count isnt there... Commented Mar 27, 2014 at 6:11
  • You can use something like this. Commented Mar 27, 2014 at 6:22
  • hi Aderiano i have add code. first to not declare a count variable and not increase value on loop of count so not work in your code so just see and compare your code. Commented Mar 27, 2014 at 7:44

3 Answers 3

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; ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

I Updated my code, can you reply? i tried add this line $totalPost but does not work :( Thanks
Now put last in every article, not 3by3 :/ u know why?
0

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

2 Comments

I Updated my code, can you reply? i tried add this line $totalPost but does not work :( Thanks
Did you put it at the place I suggested? Also, echo $totalPost; ?
0
<?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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.