我正在尝试在类别存档中创建多列布局。多亏了社区,我才能找到所有的答案,除了这个。。。我相信对于更高级的人来说,这很容易做到:)
以下是工作正常的部分:
我想创建这种类型的布局:
因此,同一类别的前四篇文章被包装在一个分区中,第五篇文章被包装在另一个分区中(不包括1-4篇文章)。。。本工程预期使用的代码:
<div class="cell medium-6 large-3 decor-list">
<?php
$first_grid_query = new WP_Query(
array(
\'posts_per_page\'=>4,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
));
while ($first_grid_query->have_posts()) : $first_grid_query->the_post(); ?>
<div class="some-content-here">
//bla bla bla
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="cell large-6 main-cont">
<?php
$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, \'ID\' );
$second_grid_query = new WP_Query(
array(
\'posts_per_page\'=>1,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
// Add the post ids from the first loop
\'post__not_in\' => $first_grid_posts_ids,
)
);
while ($second_grid_query->have_posts()) : $second_grid_query->the_post(); ?>
<div class="some-content-here">
//bla bla bla
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="cell medium-6 large-3 quote-container">
<?php if (function_exists(\'dynamic_sidebar\') && dynamic_sidebar(\'archive-cat-widget\')) : else : ?>
<?php endif; ?>
</div>
2. But when I\'m trying to add another container with posts 6-13 (and exclude posts 1-5), the code doesn\'t work - the loop starts from the post number 1 so the posts are duplicated...
代码如下:
<?php
$first_grid_query = new WP_Query(
array(
\'posts_per_page\'=>4,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
));
while ($first_grid_query->have_posts()) : $first_grid_query->the_post(); ?>
<div class="some-content-here">
//bla bla bla
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="cell large-6 main-cont">
<?php
$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, \'ID\' );
$second_grid_query = new WP_Query(
array(
\'posts_per_page\'=>1,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
// Add the post ids from the first loop
\'post__not_in\' => $first_grid_posts_ids,
)
);
while ($second_grid_query->have_posts()) : $second_grid_query->the_post(); ?>
<div class="some-content-here">
//bla bla bla
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="cell medium-6 large-3 quote-container">
<?php if (function_exists(\'dynamic_sidebar\') && dynamic_sidebar(\'archive-cat-widget\')) : else : ?>
<?php endif; ?>
</div>
<div class="cell large-6 main-cont">
<?php
$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, \'ID\' );
$second_grid_posts_ids = wp_list_pluck( $second_grid_query->posts, \'ID\' );
$third_grid_query = new WP_Query(
array(
\'posts_per_page\'=>8,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
// Add the post ids from the first and second loop
\'post__not_in\' => array(
$first_grid_posts_ids,
$second_grid_posts_ids,
)
)
);
while ($third_grid_query->have_posts()) : $third_grid_query->the_post(); ?>
<div class="some-content-here">
//bla bla bla
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
我认为问题就在这里:
$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, \'ID\' );
$second_grid_posts_ids = wp_list_pluck( $second_grid_query->posts, \'ID\' );
$third_grid_query = new WP_Query(
array(
\'posts_per_page\'=>8,
\'post_type\' => \'post\',
\'cat\' => get_query_var(\'cat\'),
// Add the post ids from the first and second loop
\'post__not_in\' => array(
$first_grid_posts_ids,
$second_grid_posts_ids,
)
)
);
当我仅排除时
$first_grid_posts_ids
或
$second_grid_posts_ids
像这样:
post__not_in\' => $first_grid_posts_ids,
或
post__not_in\' => $second_grid_posts_ids,
第一个或第二个循环中的帖子被排除在外,但当我将这些ID添加到数组中时,代码不起作用。
有什么办法解决吗?