我正在编写一个自定义帖子插件,它将自定义帖子分组显示为选项卡。每组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>