Wordpress - 依靠不同的循环

时间:2014-03-27 06:03:17

标签: php 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个帖子,然后在文章上写一个课程......

所以我可以把这段代码放在文章类上:

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

由于

3 个答案:

答案 0 :(得分: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篇帖子之后在文章中插入last课程

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

答案 1 :(得分:0)

$loop = new WP_Query($args);使用Sanjeev的建议后,$totalPost = count($loop->posts);

答案 2 :(得分: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