2014-03-27 54 views
0

我應該如何指望這個循環?WordPress的 - 在不同的循環計數

<?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(); ?> 

有人可以幫我嗎?

非常感謝

完整代碼:

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

我需要3個員額數3,把一類的文章...

所以我可以把文章類的代碼:

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

感謝

+0

哪裏是計數,顯示完整的腳本。 –

+0

我需要把這個循環計數,數不存在... –

+0

[你可以使用像這樣的東西](http://stackoverflow.com/a/19090229/741747)。 –

回答

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 

?> 

編輯:

這將後3個員額

<?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; ?> 
+0

這給了我一個錯誤在endwhile ....我會發布所有代碼 –

+0

在我的代碼有沒有時 – sanjeev

+0

我更新了我的代碼,你能回覆嗎?我試圖添加此行$ totalPost但不起作用:( 感謝 –

0

插入文章last下課後$loop = new WP_Query($args);使用Sanjeev的建議即,$totalPost = count($loop->posts);

+0

我更新了我的代碼,你能回覆嗎?我嘗試添加此行$ totalPost但不起作用:(謝謝 –

+0

你把它放在我建議的地方嗎?還有'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