我正在将使用 Bootstrap 构建的主题集成到 WordPress 中,现在我面临着水平而不是垂直显示我的帖子的挑战。该设计使用 3 列。
本站发布两栏的解决方案 ( http://perishablepress.com/two-column-horizontal-sequence-wordpress-post-order/ ) 很有帮助,但在与 3 列一起使用时,它会重复发布之前显示的帖子。
这是我的代码:
<div class="row">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<div class="col-sm-4">
<img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
<h3><?php the_field( 'description' ); ?></h3>
</div>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif; ?>
<?php $i = 0; rewind_posts(); ?>
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<div class="col-sm-4">
<img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
<h3><?php the_field( 'description' ); ?></h3>
</div>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif; ?>
<?php $i = 0; rewind_posts(); ?>
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<div class="col-sm-4">
<img src="<?php the_field('home_page_slider_image'); ?>" class="img-responsive" >
<h3><?php the_field( 'description' ); ?></h3>
</div>
<?php endif; endwhile; else: ?>
<div>Alternate content</div>
<?php endif; ?>
</div>
如有任何帮助,我们将不胜感激。
谢谢。
最佳答案
看看这个例子,它的工作方式如你所愿,并根据这个例子安排你的代码。
$i = 1;
echo "<div class='row'>\n";
while( $i <= 10 ){
echo " <div class='col-lg-4'></div>\n";
if( $i % 3 == 0 ) { echo "</div>\n<div class='row'>\n"; }
$i++;
}
echo "</div>\n";
关于php - 如何将 WordPress 帖子水平显示为 3 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21199556/