Php Wordpress循环:将3个搜索结果换行
我已经设置了我的模板,以输出col-4中的搜索条目,从而形成一个3列的布局,并且我需要将每组3列包装成一行 我已经提出了以下内容,但似乎我的PHP的开头和结尾是错误的,因为我目前在它自己的行中得到了每个1搜索条目。提前谢谢你的帮助Php Wordpress循环:将3个搜索结果换行,php,wordpress,twitter-bootstrap,Php,Wordpress,Twitter Bootstrap,我已经设置了我的模板,以输出col-4中的搜索条目,从而形成一个3列的布局,并且我需要将每组3列包装成一行 我已经提出了以下内容,但似乎我的PHP的开头和结尾是错误的,因为我目前在它自己的行中得到了每个1搜索条目。提前谢谢你的帮助 <?php if ($counter % 3 == 0) { echo '<div class="row">'; } ?> <article id="post-<?php the_ID(); ?>" class="
<?php
if ($counter % 3 == 0) {
echo '<div class="row">';
} ?>
<article id="post-<?php the_ID(); ?>" class="search-result col-md-4">
<?php
$entry_thumbnail = racks_entry_thumbnail();
$entry_class = $entry_thumbnail ? 'search-thumbnail' : 'col-sm-4 col-md-4';
?>
<?php if( $entry_thumbnail ) : ?>
<div class=" <?php echo esc_attr( $entry_class ); ?>">
<?php echo $entry_thumbnail; ?>
<header class="entry-header clearfix">
<h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title() ?></a></h2>
</header>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div>
<footer class="entry-footer">
<a class="read-more" href="<?php the_permalink() ?>"><?php _e( 'Read more', 'racks' ) ?></a>
</footer>
</div>
<?php endif;?>
<div class="clearfix"></div>
</article><!-- #post-## -->
<?php $counter++ ; echo '</div>'; ?>
不要忘记,您只需关闭可被3整除的$counter的
:
<?php
echo ($counter % 3 === 0) ? '</div>' : '';
$counter++;
?>
目前,您在循环的每一步都关闭
。还请注意,您可能希望在迭代开始时进行此检查,将其结果分配到一个变量中。例如:
<?php
$counter = 0;
while ($counter < someLimit):
$newRow = $counter % 3 === 0;
?>
// at the beginning of template:
<?= $newRow ? '<div class="row">' : ''; ?>
// output goes here
// at the end of template:
<?= $newRow ? '</div>' : ''; ?>
<?php endwhile; ?>
//在模板的开头:
//输出到这里
//在模板末尾:
另一种方法是使用输出缓冲区存储某个数组中每个元素的输出,将此数组拆分为块(使用),然后输出这些块(将每个块包装在
结构中)。您何时定义$counter
?你没有在你的代码中显示,在这个代码之前有一个循环吗?如果是,您能显示它吗?循环是在-@rnevius之前定义的,应该在哪里定义计数器?在functions.php中??请原谅,我的php是非常有限的为什么你把答案编辑成你的问题,使它基本上与其他人无关?如果你认为这个解决方案有帮助,就接受它;如果没有,请解释什么不起作用和/或为什么它对你没有用处。嘿,我尝试了这个,但现在什么也没有显示-检查器中也没有显示任何行(我编辑了我的原始帖子以包含你的代码)。谢谢你的帮助你在那里看到什么错误了吗?最初的帖子不完整(没有$counter定义,没有循环等等),所以我在回答中只展示了一些指导原则。我不知道在哪里定义计数器。我展示的代码是一个额外的模板文件,放在一个工作循环中:很抱歉,只是在没有基本了解它们的用途的情况下将它们拼接在一起并不是一种好的编程方式。如果有一个工作循环,为什么在模板中添加而?在这种情况下,您可以使用我在回答中给出的第一个提示,忽略“您可能想要”后面的所有部分。