我正在制作一个自定义旋转木马(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; ?>