多个循环:从循环2中排除循环1中返回的帖子

时间:2018-08-30 15:45:03

标签: php wordpress loops variables

我有两个wp_query循环。

第一个循环将查找特色帖子,并以与其余帖子不同的方式对其进行格式化。

然后第二个循环显示其余帖子。

我想从循环的其余部分中排除第一篇精选文章。

现在,第一篇文章的文章ID被保存为变量。

我想在第二个循环中使用该变量,并使用wp_query exclude参数。

但是据我了解,该变量在我的第一个循环结束时消失。然后,它在第二个循环中为空变量,因此不排除任何内容。

                <!-- Here's the query for the featured post -->
                <?php
                    // the arguments

                    $args = array(
                        'posts_per_page'   => '1',
                        'orderby'          => '',
                        'meta_key'        => 'featured_post',
                        'meta_value'    => '1'
                    ); ?>

                    <?php $the_query = new WP_Query($args); ?>

                    <?php if ($the_query->have_posts()) : ?>
                    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>

                    <!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
                    <?php $postid = get_the_ID(); ?>
                    <div class="small-12 columns entry" >
                        <div class="text-center" id="featured-thumbnail">

                            <a href="<?php the_permalink(); ?>">
                                <?php the_post_thumbnail('featured'); ?>
                            </a>

                            <h2>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                            </h2>

                            <p>
                                    <?php
                                    $content = get_the_content();
                                    echo wp_trim_words($content, '75');
                                    ?>
                            </p>

                        </div>
                    </div>


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


        <!-- If there is no featured post, pull in the most recent post and make it big -->
        <?php else:  ?>

                                <?php
                              // the arguments
                              $args = array(
                                'posts_per_page'   => '1',
                                'orderby'          => '',
                              ); ?>

                                    <?php $the_query = new WP_Query($args); ?>
                                    <?php if ($the_query->have_posts()) : ?>
                                    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>


                                        <!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
                                        <?php $postid = get_the_ID(); ?>
                                        <div class="small-12 columns entry" >
                                            <div class="text-center" id="featured-thumbnail">

                                                <a href="<?php the_permalink(); ?>">
                                                    <?php the_post_thumbnail('featured'); ?>
                                                </a>

                                                <h2>
                                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                </h2>

                                                <p>
                                                        <?php
                                                        $content = get_the_content();
                                                        echo wp_trim_words($content, '75');
                                                        ?>
                                                </p>

                                            </div>
                                        </div>

                                    <!-- End of the nested loop (most recent posts) -->
                                    <?php endwhile; ?>
                                    <?php endif; ?>

                        <!-- End of the featred post query loop-->
                        <?php endif; ?>

这是主循环:

<?php get_template_part('parts/content', 'featured-post'); ?>

  <!-- This ends the logic for the featured post. Now we show the rest of the posts, excluding the first one we showed -->
  <?php get_template_part('parts/content', 'category-filter'); ?>


          <?php
            // the arguments

            $args=array(
                'paged' => $paged,
              'posts_per_page' => 9,
                'post__not_in' => array($postid)
        ); ?>
            <?php $the_query = new WP_Query( $args ); ?>
            <?php if ( $the_query->have_posts() ) : ?>

              <!-- Start row that holds blocks -->
              <div class="row small-up-1 medium-up-2 large-up-3">
              <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <div class="column entry" >


              <?php if( get_the_post_thumbnail() ): ?>
              <a href="<?php the_permalink(); ?>">
              <?php the_post_thumbnail('blog'); ?>
              </a>
              <?php else : ?>

              <?php endif; ?>




              <a href="<?php the_permalink(); ?>"><h5><?php the_title(); ?></h5></a>
              <?php
              $excerpt = get_the_excerpt();
              echo wp_trim_words( $excerpt , '10', '');
              ?>



            </div>

            <?php endwhile; ?>
            </div>
            <?php wp_reset_postdata(); ?>
            <?php else:  ?>




              <p>Sorry, no more posts.</p>




            <?php endif; ?>

我该如何精确设置要在第二个循环中使用的变量?

0 个答案:

没有答案