是否为每个引导行创建具有可变数量的柱子的循环?

时间:2019-02-06 作者:Nate

我正试图使用一个自定义查询创建一个WP循环,以显示一个自定义的post类型—使用Bootstrap 4标记。让我感到复杂的是,我希望每行的帖子数量按以下方式交替排列:

第1行-一栏-全屏宽度第2行-四栏-col-md-6第3行-六栏-col-md-4目前我正在运行三个单独的循环,这会弄乱分页。i、 e.单击分页时,页面内容保持不变,但url指示用户位于第二页。感谢您的帮助。

                      <!-- Loop 1 -->
                  <?php
                    $paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
                    $args=array(
                       \'post_type\' => \'workshop\',
                       \'post_status\' => \'publish\',
                       \'posts_per_page\' => 1,
                      );

                    $my_query = null;
                    $my_query = new WP_Query($args);

                    if( $my_query->have_posts() ) {

                      $i = 0;
                      while ($my_query->have_posts()) : $my_query->the_post();
                    // modified to work with 3 columns
                    // output an open <div>
                    if($i % 1 == 0) { ?>

                        <div class="row">

                        <?php
                        }
                        ?>

                          <!-- ROW 1 -  FULL SCREEN WIDTH MARKUP -->

                      </div><!--/.row-->

                        <?php $i++;
                        if($i != 0 && $i % 1 == 0) { ?>
                   </div> <!-- End Container -->
                   <div class="clearfix"></div>

                    <?php
                     } ?>

                    <?php
                      endwhile;
                      }
                      wp_reset_postdata();
                      ?>
                    <!-- Loop Two -->
                    <?php
                                                $paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

                                                $args=array(

                                                     \'post_type\' => \'workshop\',
                                                     \'post_status\' => \'publish\',
                                                     \'posts_per_page\' => 4,
                                                     \'offset\' => 1,
                                                     \'paged\'  => $paged

                                                    );

                                                $my_query = null;
                                                $do_not_duplicate[] = $post->ID;
                                                $my_query = new WP_Query($args);

                                                if( $my_query->have_posts() ) {

                                                    $i = 0;
                                                    while ($my_query->have_posts()) : $my_query->the_post();
                                                // modified to work with 3 columns
                                                // output an open <div>
                                                if($i % 2 == 0) { ?>

                                                <div class="row no-gutters">
                                                <?php
                                                }
                                                ?>

                      <!-- ROW 2 - [FOUR POSTS] MARKUP HERE -->

                      <?php $i++;
                      if($i != 0 && $i % 2 == 0) { ?>
                        </div><!--/.row-->

                        <?php
                         } ?>



                        <?php
                          endwhile;


                          }
                          wp_reset_query();
                          ?>
                      $args=array(

                         \'post_type\' => \'workshop\',
                         \'post_status\' => \'publish\',
                         \'posts_per_page\' => 6,
                         \'offset\' => 5,
                         \'paged\'  => $paged

                        );

                      $my_query = null;
                      $do_not_duplicate[] = $post->ID;
                      $my_query = new WP_Query($args);

                      if( $my_query->have_posts() ) {

                        $i = 0;
                        while ($my_query->have_posts()) : $my_query->the_post();
                      // modified to work with 3 columns
                      // output an open <div>
                      if($i % 3 == 0) { ?>

                      <div class="row no-gutters">
                      <?php
                      }
                      ?>

                      <!-- ROW 3 - [Six Posts] Markup -->




                      <?php $i++;
                      if($i != 0 && $i % 3 == 0) { ?>
                        </div><!--/.row-->


                      <?php
                       } ?>



                      <?php
                        endwhile;


                        }
                        wp_reset_query();
                        ?>

2 个回复
最合适的回答,由SO网友:mrben522 整理而成

要做到这一点,最简单但有点麻烦的方法是在循环之前初始化一个计数器变量,然后用它来判断你在写什么帖子。大概是这样的:

    $paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;
$args = array(
    \'post_type\'      => \'workshop\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' => 11,
    \'paged\'          => $paged,
);
$customQuery = new WP_Query($args);
$counter = 1;
if ($customQuery->have_posts()) :
    while ($customQuery->have_posts()) : $customQuery->the_post();
        if ($counter === 1) { ?>
            <div class="row">
                <!-- ROW 1 -  FULL SCREEN WIDTH MARKUP -->

            </div><!--/.row-->
            <?php
        } elseif ($counter <= 5) {
            if ($counter === 2) { ?>
                <div class="row no-gutters">
            <?php }
            ?>
                <!-- ROW 2 - [FOUR POSTS] MARKUP HERE -->
            <?php if ($counter === 5) { ?>
                </div><!--/.row-->
            <?php }
        } else {
            if ($counter === 6) { ?>
                <div class="row no-gutters">
            <?php } ?>
                    <!-- ROW 3 - [Six Posts] Markup -->
            <?php if ($counter === 11) { ?>
                </div><!--/.row-->
            <?php } 
        };
        $counter++;
    endwhile;
endif;

SO网友:Qaisar Feroz

没有必要引入计数器,您可以使用$wp_query->current_post.

<?php

$paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;

$args = array(
    \'post_type\'      => \'workshop\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' => 11,
    \'paged\'          => $paged,
);

$customQuery = new WP_Query($args);

// Row 1 - One Post - Full Screen width
while ($customQuery->have_posts() && $customQuery->current_post < 1 ) : $customQuery->the_post(); ?>

            <div class="row">  
               <!-- ROW 1 -  FULL SCREEN WIDTH MARKUP -->
            </div><!--/.row-->

<?php endwhile;

//Row 2 - Four Posts - col-md-6
while ($customQuery->have_posts() && $customQuery->current_post < 5) : $customQuery->the_post(); ?>

            <div class="row no-gutters">
               <!-- ROW 2 - [FOUR POSTS] MARKUP HERE -->
            </div><!--/.row-->

<?php endwhile;

//Row 3 - Six Posts - col-md-4
while ($customQuery->have_posts()) : $customQuery->the_post(); ?>

            <div class="row no-gutters">
                <!-- ROW 3 - [Six Posts] Markup -->
            </div><!--/.row-->

<?php endwhile; ?>

相关推荐

Adding pagination to my theme

我想为我的wordpress主题添加一个分页。然而,对我来说,这并不容易,因为我的主题是从插件“AT Posts Column”构建的,该插件在主页上显示最新的帖子。我想将分页添加到此插件:)。我尝试插入分页插件,并将代码粘贴到下面的文件的末尾。确实出现了分页,但当我进入下一页时,只有www.website上的链接发生了变化。com/page/2。请帮助:)。我的网站是http://consolezone.pl插件文件“在Posts列中”:<?php /** * Custom co