wordpress - 将每 2 个 WordPress 帖子包含在 Bootstrap Row 类中

标签 wordpress twitter-bootstrap

我需要知道如何在 .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/

相关文章:

css - 以站点标题为中心的标志 Wordpress

widget - 最近的帖子 wordpress 小部件,文本有限,链接为 'read more'

javascript - WordPress 主题在 IE 8 及以下版本中显示不正确

html - 取消悬停后使 CSS 悬停状态保持不变

javascript - 在模态窗口中单击"is"后发送表单数据

javascript - 事件类不显示

PHP 循环不显示帖子

jquery - 无论悬停位置如何,全宽下拉菜单 Bootstrap

css - 图像中心网格响应

css - 导航栏折叠在 Rails 5/Bootstrap 3 上不起作用