在Bootstrap Row Class中包装每2个WordPress帖子

She*_*wel 2 wordpress twitter-bootstrap

我需要知道如何在.row类中自动添加x个WordPress帖子,因为我在Bootsrap上工作.

这是我的Posts循环代码.

                <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; ?>
Run Code Online (Sandbox Code Playgroud)

小智 13

过去,我在PHP中创建了一个计数器,并在循环结束前将其放置.每次循环运行时,计数器的值将增加1.这意味着您可以编写一段代码,如果计数器等于某个数字(或者更明显地,在一定数量的博客帖子之后),则执行某个操作.

试试这个:

      <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 -->
Run Code Online (Sandbox Code Playgroud)

在这种情况下,你说如果计数器可以被2整除(如果你的帖子有一个col-md-6类就是你需要的那个),在一个结束div标签中回显,然后打开一个新行.