我希望你们中的一位Wordpress大师可以帮助我。
我试图在循环中创建一个循环,该循环呈现12个帖子,分为6行。在每一行中你有2个div。每个div都用于显示帖子标题。循环遍历所有12个帖子并正确分组 - 6个div,2个帖子。每篇文章都有自己独特的标题。
我设法让循环将12个帖子分解为6个div,每个div中有两个内部div。但我不能让内部的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
答案 0 :(得分:0)
在这里,我将问题分解成碎片,这样你就可以看到循环工作了。
<?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?>
然后,您不是只回显一个常量<p>
标记,而是在那里放置一个循环。
<?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张幻灯片,每张幻灯片包含两个帖子。
我发布的代码是相对于最终解决方案的骨架。但是,希望这种“计数器”方法可以帮助您为每个轮播幻灯片分配正确数量的帖子。
答案 1 :(得分:0)
谢谢大家的帮助。我设法解决了这个问题!这是解决方案:
如果有人有任何想法让这个更好的解决方案知道!干杯
<?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();
?>