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条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题