如何在Wordpress中使用排除功能进行第二个循环



single.php有两个循环,一个用于显示选定的帖子,另一个用于显示所有其他帖子。现在我想排除从第一个循环返回的帖子。

我发现这个片段<?php query_posts(array('post__not_in' => array($thePostID))); ?>

我不知道如何将其合并到我的代码中,或者是否有另一种方法可以解决这个问题

这是我的单.php代码:

<?php get_header(); ?>
<div id="primary" class="site-content">
    <div id="content" role="main">
        <?php while ( have_posts() ) : the_post(); ?>

     <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <?php the_content(); ?>
    </div>
            <?php comments_template( '', true ); ?>
        <?php endwhile; // end of the loop. ?>
    <?php wp_reset_postdata(); // reset the post data so we can run another query ?>
 <?php 
 $args_second = array(
        'posts_per_page' => -1,
    );
 // The Second Query
 $second_query = new WP_Query( $args_second );
 // The Loop
 if ( $second_query->have_posts() ):
    while ( $second_query->have_posts() ):
        $second_query->the_post(); ?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <?php the_content(); ?>
    </div>
        <?php endwhile; ?>
        <?php endif; ?>
    <?php wp_reset_postdata(); // Restore original Post ?>
    </div><!-- #content -->
</div><!-- #primary -->

请尝试使用此代码代替您的单个.php代码。测试它。

<?php get_header(); ?>
<div id="primary" class="site-content">
    <div id="content" role="main">
<?php 
 $firstPosts = array();
while ( have_posts() ) : the_post(); ?>
$postid = get_the_ID();
$firstPosts[] = $postid;
     <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <?php the_content(); ?>
    </div>
            <?php comments_template( '', true ); ?>
        <?php endwhile; // end of the loop. ?>
    <?php wp_reset_postdata(); // reset the post data so we can run another query ?>
 <?php 
 //$args_second = array(
   //     'posts_per_page' => -1,
   // );
 // The Second Query
 $second_query = new WP_Query( array('post__not_in' => $firstPosts, 'order' => 'ASC', 'posts_per_page' => -1 ));
 // The Loop
 if ( $second_query->have_posts() ):
    while ( $second_query->have_posts() ):
        $second_query->the_post(); ?>
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
        <?php the_content(); ?>
    </div>
        <?php endwhile; ?>
        <?php endif; ?>
    <?php wp_reset_postdata(); // Restore original Post ?>
    </div><!-- #content -->
</div><!-- #primary -->

相关内容

最新更新