Increase offset while looping

时间:2018-05-09 作者:Dariusz

我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组4个岗位。是否可以编写一个偏移量随每次循环而增加的查询?因此,结果将是:
-第一个查询显示从1到4的帖子
-第二个查询显示从5到8的帖子
-第三个查询显示从9到12的帖子等。

        <div class="official-matters-tabs">
          <?php $args = array(\'post_type\' => \'official-matters\', \'showposts\' => 4, \'orderby\' => \'date\', \'order\' => \'ASC\',); $the_query = new WP_Query( $args ); ?>
          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
          <div class="info-tab col-xl-3">
            <div class="info-img">
              <?php the_post_thumbnail(); ?>
            </div><!-- .info_img -->
            <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" class="info-title" role="button" aria-expanded="false" aria-controls="collapse-<?php the_ID(); ?>">
              <?php the_title(); ?>
            </a>
          </div><!-- .info-tab -->
        <?php endwhile;?>
        <?php wp_reset_postdata(); ?>
        </div><!-- .official-matters-tabs -->

        <div class="official-matters-content">
          <?php $args = array(\'post_type\' => \'official-matters\', \'showposts\' => 4, \'orderby\' => \'date\', \'order\' => \'ASC\',); $the_query = new WP_Query( $args ); ?>
          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
          <div class="info-tab-content collapse" id="collapse-<?php the_ID(); ?>">
            <div class="card card-body">
                <?php the_excerpt(); ?>
            </div><!-- .card -->
          </div><!-- .info-tab-content -->
        <?php endwhile;?>
        <?php wp_reset_postdata(); ?>
        </div><!-- .official-matters-content -->

      </div><!-- .official-matters-group -->
  </div><!-- #collapse-official-matters -->
<小时>

UPDATED

我做了你建议的更改。目前查询中最大的问题是我得到的输出:

<div class="official-matters-group">
 <div class="official-matters-tabs">
  <div class="info-tab col-xl-3">
    ...
  </div>
</div>
<div class="official-matters-content">
 <div class="info-tab-content collapse">
  ...
 </div>
 <div class="official-matters-tabs">
   <div class="info-tab col-xl-3">
   ...
   </div>
</div>
<div class="official-matters-content">
   <div class="info-tab-content collapse">
   ...
   </div>
   <div class="official-matters-tabs">
      <div class="info-tab col-xl-3">
      ...
      </div>
   </div>
</div>

我需要的是:

<div class="official-matters-group">
 <div class="official-matters-tabs">
  <div class="info-tab col-xl-3">
   ...
  </div>
  <div class="info-tab col-xl-3">
   ...
  </div>
  <div class="info-tab col-xl-3">
   ...
  </div>
  <div class="info-tab col-xl-3">
   ...
  </div>
 </div>
 <div class="official-matters-content">
  <div class="info-tab-content collapse">
   ...
  </div>
  <div class="info-tab-content collapse">
   ...
  </div>
  <div class="info-tab-content collapse">
   ...
  </div>
  <div class="info-tab-content collapse">
   ...
  </div>
</div> 
我需要将4个帖子分组在官方事务组中,并循环多次,直到所有帖子都显示出来。我不知道该怎么做。

现在我的查询代码如下所示:

        <?php global $duplicated_posts;
          $args = [
              \'post_type\' => \'official-matters\',
              \'showposts\' => 20,
              \'orderby\' => \'date\',
              \'order\' => \'ASC\',
              \'post__not_in\' => $duplicated_posts
          ];
          $query = new \\WP_Query($args); ?>

          <div class="official-matters-group">
            <?php if( $query->have_posts() ) : ?>
              <?php while( $query->have_posts() ) : $query->the_post();
                    $duplicated_posts[] = get_the_ID();
              ?>
                <div class="official-matters-tabs">
                  <div class="info-tab col-xl-3">
                    <div class="info-img">
                      <?php the_post_thumbnail(); ?>
                    </div><!-- .news_img -->
                    <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" class="info-title" role="button" aria-expanded="false" aria-controls="collapse-<?php the_ID(); ?>">
                      <?php the_title(); ?>
                    </a>
                  </div>
                </div><!-- .official-matters-tabs -->

                <div class="official-matters-content">
                  <div class="info-tab-content collapse" id="collapse-<?php the_ID(); ?>">
                    <div class="card card-body">
                        <?php the_content(); ?>
                    </div><!-- .card -->
                  </div><!-- .info-tab-content -->
                 </div><!-- .offical-matters-content -->
                <?php
                endwhile;
                wp_reset_postdata();
              endif;
            ?>
        </div><!-- .official-matters-group -->
    </div><!-- .collapse-official-matters -->
<小时>

UPDATED

到目前为止,我做到了这一点:

<div id="collapse-official-matters" class="col-xl-12">
   <div class="official-matters-group">

     <?php
      $args = array(
        \'post_type\' => \'sprawy-urzedowe\',
        \'showposts\' => 20,
        \'orderby\' => \'date\',
        \'order\' => \'ASC\',
        \'post__not_in\' => $duplicated_posts
     );

     $the_query = new WP_Query($args);
     if ($the_query->have_posts()) :
       $counter = 0;
       while ($the_query->have_posts()) : $the_query->the_post();
           if ($counter % 4 == 0) :
               echo $counter > 0 ? \'</div>\' : \'\';
               echo \'<div class="official-matters-tabs">\';
           endif;
           ?>

           <div class="info-tab col-xl-3">
             <div class="info-img">
               <?php the_post_thumbnail(); ?>
             </div><!-- .news_img -->
             <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" class="info-title" role="button" aria-expanded="false" aria-controls="collapse-<?php the_ID(); ?>">
               <?php the_title(); ?>
             </a><!-- .info-title -->
           </div><!-- .info-tab -->

     <?php
       $counter++;

       endwhile;
       endif;
       wp_reset_postdata();
     ?>
   </div>


   </div><!-- .official-matters-group -->
</div><!-- .collapse-official-matters -->
但我不知道如何添加下面的代码以获得所需的输出

<div class="official-matters-content">
  <div class="info-tab-content collapse" id="collapse-<?php the_ID(); ?>">
    <div class="card card-body">
        <?php the_content(); ?>
    </div><!-- .card -->
  </div><!-- .info-tab-content -->
</div>

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

经过很长时间,我们找到了解决办法。也许这会对将来的人有所帮助。

  <div id="collapse-official-matters" class="col-xl-12">
       <div class="official-matters-group">

         <?php $args = array(\'post_type\' => \'official-matters\', \'showposts\' => 20, \'orderby\' => \'date\', \'order\' => \'ASC\',); $the_query = new WP_Query( $args ); ?>
         <?php $i = 0; $row = 0;
          while ( $the_query->have_posts()) {

            $the_query->the_post();
            if ($i === 4) {
              $i = 0;
              $row++;
            }

            $postsData[$row][\'tabs\'][$i] = [
              \'id\' => get_the_id(),
              \'thumbnail\' => get_the_post_thumbnail_url(),
              \'title\' => get_the_title()
            ];

            $postsData[$row][\'content\'][$i] = [
              \'id\' => get_the_id(),
              \'excerpt\' => get_the_excerpt()
            ];

            $i++;
          }
         ?>

         <div id="accordion">
         <?php foreach($postsData as $key => $value) :
           $tabs = $value[\'tabs\'];
           $contents = $value[\'content\'];
           ?>
             <div class="customRow">
               <?php foreach($tabs as $key => $value) : ?>
                 <div class="info-tab col-xl-3">
                   <div class="info-img">
                     <img src="<?php echo $value[\'thumbnail\'] ?>" />
                   </div><!-- .news_img -->
                   <a data-toggle="collapse" data-target="#collapse-<?php echo $value[\'id\'] ?>" href="#collapse-<?php echo $value[\'id\'] ?>" class="info-title collapsed" role="button" aria-expanded="false" aria-controls="collapse-<?php echo $value[\'id\'] ?>">
                     <?php echo $value[\'title\'] ?>
                   </a><!-- .info-title -->
                 </div><!-- .info-tab -->
               <?php endforeach; ?>

               <?php foreach($contents as $key => $value) : ?>
                 <div class="official-matters-content">
                   <div class="info-tab-content collapse" id="collapse-<?php echo $value[\'id\']; ?>" data-parent="#accordion">
                     <div class="card card-body">
                       <?php echo $value[\'excerpt\'] ?>
                     </div><!-- .card -->
                   </div><!-- .info-tab-content -->
                 </div><!-- .official-matters-content -->
               <?php endforeach; ?>
             </div>
           <?php endforeach ;?>
       </div>
         <?php wp_reset_postdata(); ?>

         <?php if (false) : ?>
         <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
               <div class="info-tab col-xl-3">
                 <div class="info-img">
                   <?php the_post_thumbnail(); ?>
                 </div><!-- .news_img -->
                 <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" class="info-title" role="button" aria-expanded="false" aria-controls="collapse-<?php the_ID(); ?>">
                   <?php the_title(); ?>
                 </a><!-- .info-title -->
               </div><!-- .info-tab -->

               <div class="official-matters-content">
                 <div class="info-tab-content collapse" id="collapse-<?php the_ID(); ?>">
                   <div class="card card-body">
                       <?php the_excerpt(); ?>
                   </div><!-- .card -->
                 </div><!-- .info-tab-content -->
               </div><!-- .official-matters-content -->


         <?php
           endwhile;
           wp_reset_postdata();
         ?>
       <?php endif; ?>

       </div><!-- .official-matters-group -->
    </div><!-- .collapse-official-matters -->

SO网友:coolpasta

是的。

你要找的是一个智能的“计数器”。

实际上,您的问题是:

You\'re trying to avoid duplicate posts.

这里有一种流行的方法。

在运行查询的每个文件中,必须添加以下代码:

global $duplicated_posts;

$args = [
    \'post_type\' => \'post\',
    \'post__not_in\' => $duplicated_posts
];

$query = new \\WP_Query($args);
请注意post__not_in. 现在,这个文件将运行一次(假设在页面生成器中运行,但无所谓),当您包含它时,让我们继续。

“可以,但数组仍然为空”。

当您实际检查是否有帖子时,必须将找到的每个帖子的ID添加到该全局数组中,如下所示:

if( $query->have_posts() ) : ?>
    <div class="official-matters-group">
        <div class="official-matters-tabs">
        ..etc
        <?php
        while( $query->have_posts() ) :
            $query->the_post();
            $duplicated_posts[] = get_the_ID(); //golden ticket
            ..code for each post goes here.
现在,当你的下一个内容框看到这个$duplicated_posts, 它将知道不包括这些帖子。

请记住,您必须在每个文件中复制此模式,以避免重复项目!

干杯

结束

相关推荐

对类别使用WP_QUERY而不是GET_TERMS

我已通过此功能从Woocommerce获取所有父类别$terms = get_terms( array( \'taxonomy\' => \'product_cat\', \'hide_empty\' => false, \'parent\' => 0 ) ); 但我还没有做到这一点WP_Query. 现在我有两个问题:如何使用WQ\\u Query从WooCommerce获取类别列表?建议使用WP\\u Query