1 回答
TA贡献1828条经验 获得超13个赞
问题是:它不能像这样使用模 % 。这是一种打开包装 div 并在以交替方式打印恰好 2 个元素后关闭它的技术:
$i:
0: open, element 1
1: element 2, close
2: open, element 3,
3: element 4, close
对于 % 3,它将产生:
$i:
0: open, element 1
1: element 2, close
2: element 3, close
3: open, element 4
4: element 5, close
5: element 6, close
因此,关闭标签的数量是打开标签的两倍<div>。
要解决此问题,您必须更改if条件,如下所示:
<div class="news-slider">
<?php
$i = 0;
$numItems = 3; // Change the number of items per slide here
?>
<?php $the_query = new WP_Query( 'cat=8,7,9&posts_per_page=6' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<?php if ( $i % $numItems == 0) : ?>
<div class="wrap">
<?php endif; ?>
<div class="news-snippet">
<div class="news-snippet-thumbnail" style="background: url('<?php echo $edTheDev = $backgroundImg[0] ? $backgroundImg[0] : '/wp-content/themes/quantinsight/assets/img/post-thumb.png'; ?>') no-repeat center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"></div>
<div class="news-snippet-content">
<h3 class="[ f-avenir-book-26 u-ColorBlue ]"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<p class="news-snippet-date"><?php echo the_time('d.m.y'); ?></p>
<p class=""><a href="<?php the_permalink() ?>">Read More</a></p>
</div>
</div>
<?php if ( ($i + 1) % $numItems == 0 ) : ?>
</div>
<?php endif; ?>
<?php
$i++;
endwhile;
wp_reset_postdata();
?>
</div>
这是通过更改换行关闭 if 条件来实现的,以便它在$numItem打开后的每次迭代后触发。您可以配置$numItems为任意正数的项目。
- 1 回答
- 0 关注
- 68 浏览
添加回答
举报