我需要知道如何在 .row 类中自动添加 x 数量的 WordPress 帖子,因为我在 Bootsrap 上工作。
这是我的帖子循环代码。
<div id="main" class="container" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-6" id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?> role="article">
<header class="article-header">
<h1 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
<p class="byline vcard"><?php
printf( __( 'Posted <time class="updated" datetime="%1$s" pubdate>%2$s</time> by <span class="author">%3$s</span> <span class="amp">&</span> filed under %4$s.', 'bonestheme' ), get_the_time('Y-m-j'), get_the_time(get_option('date_format')), bones_get_the_author_posts_link(), get_the_category_list(', '));
?></p>
</header>
<section class="entry-content clearfix">
<?php the_content(); ?>
</section>
<footer class="article-footer">
<p class="tags"><?php the_tags( '<span class="tags-title">' . __( 'Tags:', 'bonestheme' ) . '</span> ', ', ', '' ); ?></p>
</footer>
</article>
<?php endwhile; ?>
最佳答案
过去,我在 PHP 中创建了一个计数器,并将其放置在循环结束之前。每次循环运行时,计数器的值都会加一。这意味着您可以编写一段代码,表示如果计数器等于某个数字(或者更简单地说,在一定数量的博客文章之后),则执行某个操作。
试试这个:
<div id="main" class="container" role="main">
<div class="row">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-6" id="post-<?php the_ID(); ?>" role="article">
<!-- loop content -->
</article>
<?php $counter++;
if ($counter % 2 == 0) {
echo '</div><div class="row">';
}
endwhile; endif; ?>
</div><!-- /row -->
</div><!-- /container -->
在本例中,您是说如果计数器可以被 2 整除(如果您的帖子具有 col-md-6 类,则需要这样做),则在结束 div 标记中回显,然后打开一个新行。
关于wordpress - 将每 2 个 WordPress 帖子包含在 Bootstrap Row 类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496052/