如果只有一个帖子,则隐藏旋转木马指示器

时间:2015-02-24 作者:Johann

我正在制作一个自定义旋转木马(Wordpress+Bootstrap)。旋转木马工作正常。然而,如果只有一个帖子,我想隐藏引导转盘指示器和箭头。

我可以使用以下代码隐藏旋转木马箭头:

<?php if ($counter > 1) { ?>

      <a class="left carousel-control" href="#testimonials" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
      <a class="right carousel-control" href="#testimonials" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>

<?php } ?>
但是,我无法访问旋转木马指示器所在的$计数器变量。我试着在一段时间内使用一段时间,但页面断了。

下面是有问题的代码:

  <ol class="carousel-indicators">

    <?php $indicatorCount = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <li data-target="#testimonials" data-slide-to="<?=$indicatorCount?>" class="<?php echo ($indicatorCount==0) ? "active" : "" ?>"></li>
      <?php echo $indicatorCount; ?>

    <?php ++$indicatorCount; endwhile; ?>

  </ol> <!-- /.carousel-indicators --> 
这是我的完整代码(仅供参考)

  <?php

  $args = array(

    \'post_type\' => \'post\', \'posts_per_page\' => -1, \'orderby\' => \'menu_order\', \'order\' => \'ASC\',

  );

  $loop = new WP_Query( $args );

  ?>

  <?php if( $loop->have_posts() ): ?>

      <div id="testimonials" class="carousel sp slide carousel-fade" data-ride="carousel" >

      <ol class="carousel-indicators">

        <?php $indicatorCount = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <li data-target="#testimonials" data-slide-to="<?=$indicatorCount?>" class="<?php echo ($indicatorCount==0) ? "active" : "" ?>"></li>
          <?php echo $indicatorCount; ?>

        <?php ++$indicatorCount; endwhile; ?>

      </ol> <!-- /.carousel-indicators --> 

        <div class="carousel-inner">

        <?php $counter = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="item <?php echo ($counter==0) ? "active" : "" ?>">

            <div class="row">

              <div class="col-md-4">

                <img class="img-responsive" src="http://placehold.it/360x200" />

              </div>

              <div class="col-md-8">

                <h3><?php the_title(); ?></h3>

                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>

              </div>

            </div> <!-- /.row --> 

          </div> <!-- /.item --> 

        <?php $counter++; endwhile; ?>        

          <?php if ($counter > 1) { ?>

            <a class="left carousel-control" href="#testimonials" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
            <a class="right carousel-control" href="#testimonials" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>

          <?php } ?>

        </div> <!-- /.carousel-inner --> 

      </div> <!-- #testimonials .carousel --> 

  <?php endif; ?>
如果只有一个帖子,我如何隐藏旋转木马指示器块?

非常感谢您的投入和帮助!

更新:这是代码和提供的答案!

  <?php

  $args = array(

    \'post_type\' => \'post\', \'posts_per_page\' => -1, \'orderby\' => \'menu_order\', \'order\' => \'ASC\',

  );

  $loop = new WP_Query( $args );

  ?>

  <?php if( $loop->have_posts() ): ?>

      <div id="testimonials" class="carousel sp slide carousel-fade" data-ride="carousel" >

      <?php if($loop->post_count > 1) { ?>

      <ol class="carousel-indicators">

        <?php $indicatorCount = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <li data-target="#testimonials" data-slide-to="<?=$indicatorCount?>" class="<?php echo ($indicatorCount==0) ? "active" : "" ?>"></li>

        <?php ++$indicatorCount; endwhile; ?>

      </ol> <!-- /.carousel-indicators --> 

      <?php } ?>

        <div class="carousel-inner">

        <?php $counter = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>

          <div class="item <?php echo ($counter==0) ? "active" : "" ?>">

            <div class="row">

              <div class="col-md-4">

                <img class="img-responsive" src="http://placehold.it/360x200" />

              </div>

              <div class="col-md-8">

                <h3><?php the_title(); ?></h3>

                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>

              </div>

            </div> <!-- /.row --> 

          </div> <!-- /.item --> 

        <?php $counter++; endwhile; ?>        

          <?php if($loop->post_count > 1) { ?>

            <a class="left carousel-control" href="#testimonials" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
            <a class="right carousel-control" href="#testimonials" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>

          <?php } ?>

        </div> <!-- /.carousel-inner --> 

      </div> <!-- #testimonials .carousel --> 

  <?php endif; ?>

2 个回复
最合适的回答,由SO网友:Abhisek Malakar 整理而成

您可以使用条件

<?php
    if($loop->post_count > 1){
        /**
          * so show the carousel counter
         */

    }
?>

SO网友:Kobus Beets

这是一个我过去也使用过的解决方案。这使我可以在页面上的其他位置以及平铺的图库或缩略图等中使用幻灯片。

<?php 
$indicators = array();
$slides = array();

$counter = 0;
while ( $loop->have_posts() ) : $counter++;
    array_push($indicators, \'<li data-target="#testimonials" data-slide-to="\'.$counter.\'" class="\'.(($counter == 0) ? "active" : "").\'"></li>\');
    array_push($slides, $loop->the_post());
endwhile; 

//show the slide indicators
if(count($indicators) > 1) { ?>
    <ol class="carousel-indicators">
    <?php foreach($indicators as $indicator):
        echo $indicator;
    endforeach; ?>
    </ol>
<?php } 

//show the left and right controls
if(count($indicators) > 1) { ?>
    <a class="left carousel-control" href="#testimonials" role="button" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
    <a class="right carousel-control" href="#testimonials" role="button" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>
<?php } 

//show the slides
if(count($slides) > 0):
    foreach($slides as $slide):
        //the way I used to print the slides
    endforeach;
endif; ?>

结束

相关推荐

Wordpress HTTPS redirect loop

我一直在努力让HTTPS在我的网站上发挥作用,现在已经有十多个小时了。我头痛得厉害,想自己治好。我想让你明白,我尝试了几种不同的方法来解决这个令人惊叹的社区,只是作为最后的手段。如果有人能帮助我,我将不胜感激。该网站在Apache上运行,conf文件如下所示:# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [