php - Bootstrap 4 Carousel 使用 WP Posts 堆叠而不是滑动
问题描述
我想将 Wordpress 帖子项目显示为滑块,但它们似乎在堆叠而不是从左向右滑动,因此轮播控件不起作用。如果我让'posts_per_page' => 1,第二个帖子就会消失,但如果我让'posts_per_page' => 10,它们会堆叠。任何帮助将不胜感激。非常感谢,卡利
$post = get_field('post');
?>
<!-- OPEN CAROUSEL LOOP -->
<?php
$loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderyby' => 'post_id',
'order' => 'ASC',
)); ?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- START CAROUSEL **************************************************************** -->
<div id="news-carousel" class="carousel slide" data-ride="carousel">
<!-- **************************************************************** -->
<!-- Indicators -->
<ul class="carousel-indicators">
<?php
$active = 'active';
$num = 0;
?>
<li data-target="#carousel1" data-slide-to="<?php echo $num ?>" class="<?php echo $active ?>"></li>
<?php
$active = '';
$num += 1;
?>
</ul>
<!-- **************************************************************** -->
<?php if ( has_post_thumbnail() ) { ?>
<!-- The slideshow -->
<div class="carousel-inner" role="listbox" >
<?php $active = 'active'; ?>
<div class="carousel-item <?php echo $active ?>">
<div >
<?php the_post_thumbnail( 'full' ); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
</div>
</div><!-- /item -->
<?php $active = ''; ?>
</div>
<!-- // End The slideshow -->
<?php } ?>
<!-- **************************************************************** -->
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#news-carousel" role="button" data-slide="prev">
<i class="fa fa-chevron-left"></i> </a>
<a class="carousel-control-next" href="#news-carousel" role="button" data-slide="next">
<i style="color: black;" class="fa fa-chevron-right"></i> </a>
</div> <!-- Carousel 1 -->
<!-- End Carousel **************************************************************** -->
<?php
endwhile;
wp_reset_postdata();
?>
解决方案
Try to use this code:
<!--OPEN CAROUSEL LOOP-->
<?php $loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'orderyby' => 'post_id',
'order' => 'ASC' ));
?>
<!--CAROUSEL indicators SECTION START HERE-->
<ul class="carousel-indicators">
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li data-target="#carousel1" data-slide-to="<?php echo $count ?>" class="<?php if($count == '0'){ echo 'active'; } ?>"></li>
<?php $count++; endwhile; wp_reset_postdata(); ?>
</ul>
<!--//CAROUSEL indicators SECTION END HERE-->
<!--CAROUSEL SLIDER SECTION START HERE-->
<div id="news-carousel" class="carousel slide" data-ride="carousel">
<?php $count = 0; while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<!-- The slideshow -->
<div class="carousel-inner" role="listbox" >
<div class="carousel-item <?php if($count == '0'){ echo 'active'; } ?>">
<div >
<?php the_post_thumbnail( 'full' ); ?>
<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
</div>
</div><!-- /item -->
</div>
<!-- // End The slideshow -->
<?php } ?>
<!-- **************************************************************** -->
<?php $count++; endwhile; wp_reset_postdata(); ?>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#news-carousel" role="button" data-slide="prev">
<i class="fa fa-chevron-left"></i> </a>
<a class="carousel-control-next" href="#news-carousel" role="button" data-slide="next">
<i style="color: black;" class="fa fa-chevron-right"></i> </a>
</div> <!-- Carousel 1 -->
<!--//CAROUSEL SLIDER SECTION END HERE-->
<!--//CAROUSEL indicators SECTION End HERE-->
推荐阅读
- javascript - 为什么动态删除多个输入文本不起作用?
- spring-mvc - 如果我不在配置文件中提供 handlerMapping,我的应用程序会工作吗?
- ruby-on-rails - 邮递员发布请求工作正常,但导出了 Ruby Don
- python - 使用 JWT-Extended 生成 JSON Web 签名以供用户确认
- node.js - 将 TypeScript 项目发布到没有源代码的 NPM
- java - 在 MVC 模型中以 JSON 格式显示查询结果
- microsoft-teams - App Studio 不支持清单版本 1.5
- android - Kivy 和 Google Play 64 位
- azure-logic-apps - 在 Azure 逻辑应用中定义多个相同数据类型的变量
- mongodb - 通过具有多个名称的标识符更新匹配的数组