douchuitang0642 2013-09-23 21:32
浏览 49
已采纳

PHP循环wordpress中的帖子数量

I need to create a slider navigation which has one li for each post. 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

  • 写回答

2条回答 默认 最新

  • douluan1533 2013-09-23 21:41
    关注

    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;

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法