Php 在Wordpress中的循环中创建一个循环,以显示包含在单独div中的12篇文章,每组两篇
我希望你们当中的一位Wordpress大师能在这里帮助我 我试图在一个循环中创建一个循环,该循环在6行中显示12篇文章。每行有2个div。每个div都要显示一个帖子标题。循环贯穿了所有12篇文章,并对它们进行了正确的分组——6个div,2篇文章。每个帖子都有自己独特的标题 我已经设法让循环将12篇文章分解成6个分区,每个分区都有两个内部分区。但是我不能让内部的div在所有的帖子中循环。相反,它们只是在前两个循环 所以我最后得到的是6行,每行有两个div。但只有前两个帖子在所有行中不断重复出现。我做错了什么Php 在Wordpress中的循环中创建一个循环,以显示包含在单独div中的12篇文章,每组两篇,php,wordpress,bootstrap-carousel,Php,Wordpress,Bootstrap Carousel,我希望你们当中的一位Wordpress大师能在这里帮助我 我试图在一个循环中创建一个循环,该循环在6行中显示12篇文章。每行有2个div。每个div都要显示一个帖子标题。循环贯穿了所有12篇文章,并对它们进行了正确的分组——6个div,2篇文章。每个帖子都有自己独特的标题 我已经设法让循环将12篇文章分解成6个分区,每个分区都有两个内部分区。但是我不能让内部的div在所有的帖子中循环。相反,它们只是在前两个循环 所以我最后得到的是6行,每行有两个div。但只有前两个帖子在所有行中不断重复出现。我
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
<div class="section section-testimonials">
<?php
$args=array(
'post_type' => 'testimonial'
);
$query = null;
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2 ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- Two posts appear here -->
<div class="col-md-6">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>
</div>
<!--TESTER -->
<!--TESTER -->
<!--TESTER -->
任何帮助都将不胜感激
干杯,
Sanny在这里,我将问题分解为几个部分,这样您就可以看到循环自己工作
<?php
$slides = [0,1,2,3,4,5];
foreach($slides as $slide):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<p>Example Carousel Slide <?php echo $slide;?></p>
</div>
<?php endforeach?>
转盘滑梯示例
然后,不只是重复一个常量
标记,而是在那里放置一个循环
<?php
$posts = ["post1", "post2"];
foreach($posts as $post):?>
<div class="col-md-6">
<?php echo $post;?>
</div>
<?php endforeach?>
我会向前跳几步,但你会得到这样的结果
<?php
$posts = [
0=>"post0",
1=>"post1",
2=>"post2",
3=>"post3",
4=>"post4",
5=>"post5",
6=>"post6",
7=>"post7",
8=>"post8",
9=>"post9",
10=>"post10",
11=>"post11"];
for($slideNumber=0; $slideNumber < 6; $slideNumber++):
?>
<!-- I need to get 6 of these divs which contain 2 cards inside them -->
<div class="item active">
<?php
echo("<p>This is slide number " . $slideNumber . "</p>");
for($i=0; $i<2; $i++):?>
<div class="col-md-6">
<?php
$actualPostNumber= ($slideNumber * 2) + $i ;
echo("<p>" . $posts[$actualPostNumber] . "</p>");
?>
</div>
<?php endfor; ?>
</div>
<?php endfor; ?>
阅读该代码,并尝试对它将产生的结果提出期望。最后,你应该得到6张幻灯片,每张幻灯片包含两篇文章
我发布的代码是相对于最终解决方案的框架。不过,希望这种“计数器”方法能帮助您为每个旋转木马幻灯片分配正确数量的帖子。谢谢大家的帮助。我已经设法解决了这个问题!!!以下是解决方案: 如果有人想让这成为一个更好的解决方案,让我知道!干杯
<?php
$args=array(
'post_type' => 'testimonial',
);
$query = new WP_Query($args);
if( $query -> have_posts() ) {
echo '';
$i = 0;
$ids = array();
while ($query -> have_posts()) : $query->the_post();
if($i % 2 == 0) { ?>
<div class="item <?php if($i == 0) :?>active<?php endif; ?>">
<div class="row">
<?php $loop = new WP_Query( array( 'post_type' => 'testimonial', 'posts_per_page' => 2, 'orderby' => 'rand', 'post__not_in' => $ids ) ); ?>
<?php while ( $loop -> have_posts() ) : $loop -> the_post(); ?>
<!-- A single testimonial -->
<div class="col-md-6">
<div class="card card-plain">
<div class="content">
<h4 class="title"><?php the_content(); ?></h4>
<div class="footer">
<div class="author">
<span><?php the_title(); ?></span>
</div>
</div>
</div>
</div>
</div>
<?php $ids[] = get_the_ID(); ?>
<?php endwhile; ?>
</div>
</div>
<?php } ?>
<?php
$i++;
endwhile;
}
wp_reset_query();
?>
您可以使用计数器…但引导网格应按原样显示/包装。