自定义循环和wp_list_pluck的问题

时间:2017-12-16 作者:Anda

我正在尝试在类别存档中创建多列布局。多亏了社区,我才能找到所有的答案,除了这个。。。我相信对于更高级的人来说,这很容易做到:)

以下是工作正常的部分:

我想创建这种类型的布局:

Layout 01

因此,同一类别的前四篇文章被包装在一个分区中,第五篇文章被包装在另一个分区中(不包括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...

Layouy02

代码如下:

                    <?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添加到数组中时,代码不起作用。

有什么办法解决吗?

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

问题解决了!

您需要在第三次查询之前更改这部分代码。。。它将合并您以前的查询并从中排除帖子:

$first_grid_posts_ids = wp_list_pluck( $first_grid_query->posts, \'ID\' ); 
$second_grid_posts_ids = wp_list_pluck( $second_grid_query->posts, \'ID\' ); 
$allTheIDs = array_merge( $first_grid_posts_ids , $second_grid_posts_ids );
$third_grid_query = new WP_Query(
                        array(
                            \'posts_per_page\'=>4,
                            \'post_type\' => \'post\',
                            \'cat\' => get_query_var(\'cat\'),
                            // Add the post ids from the first and second loop
                            \'post__not_in\' => $allTheIDs
                        )
                    );

结束

相关推荐

使用自己的php脚本从wp标题批量生成wp分类标签

我想使用插件将wp标题生成到标签中,https://wordpress.org/plugins/wp-full-auto-tags-manager/,因为我有数百万个帖子,所以日程表/cronjob工作不正常。所以我想用self-php脚本手动执行,我可以使用插件中的一段代码并在ssh终端上独立运行它吗,这是一段代码:$nsw_sql_gen_tags = $wpdb - > prepare(\" SELECT ID, post_title FROM $wpdb->posts WHERE po