简体   繁体   English

PHP循环获取wordpress中的帖子数量

[英]PHP loop for number of posts in wordpress

I need to create a slider navigation which has one li for each post. 我需要创建一个滑块导航,每个帖子有一个li I currently have this code: 我目前有以下代码:

<?php
$args = array( 'post_type' => 'slides', 'orderby' => 'menu_order');
$loop = new WP_Query( $args );
?>

<div id="myCarousel" class="carousel slide">
  <ol class="carousel-indicators">

    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <?php while ( $loop->have_posts() ) : $loop->the_post();  $x = 1 ?>
    <li data-target="#myCarousel" data-slide-to="<?php echo $x ?>"></li>
    <?php $x = $x + 1 ?>
    <?php endwhile; ?>

  </ol>

As I need the first one to stay active.. But this isn't quite working for me 因为我需要第一个保持活跃。.但是,这对我来说并不是很有效

try this: 尝试这个:

** take note that the $x variable was moved outside the loop so that your data-slide-to value will not all be equal to 1; **请注意,$ x变量已移出循环,因此您的data-slide-to值将不会全部等于1;

<div id="myCarousel" class="carousel slide">
    <ol class="carousel-indicators">

        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <?php $x = 1; ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li data-target="#myCarousel" data-slide-to="<?php echo $x++; ?>"></li>
        <?php endwhile; ?>
    </ol>
</div>

** if you are getting extras, it maybe because you have put a static **如果您获得额外福利,可能是因为您放置了静态物品

<li data-target="#myCarousel" data-slide-to="0" class="active"></li>

inside the loop, so what you might want could be this instead: 在循环中,因此您可能想要的是:

<div id="myCarousel" class="carousel slide">
    <ol class="carousel-indicators">
        <?php $x = 0; ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li data-target="#myCarousel" data-slide-to="<?php echo $x; ?>" <?php echo ($x++==0)?'class="active"':'';?>></li>
        <?php endwhile; ?>
    </ol>
</div>

You can get the count like this 你可以像这样得到计数

$posts = new WP_Query( $postargs );
$postcount = $posts ->post_count;

Now do echo $postcount; 现在echo $postcount; and you will have the number of posts. 您将获得职位数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM