在我的首页中,我有两个循环:
第一个是针对所有类别中最近的20篇文章,第二个是针对一个特定类别中最近的6篇文章,如何从第二个循环(特定类别循环)中删除第一个循环(常规循环)中已经出现的内容?
我知道我必须在第二个循环中使用post\\u not\\u in=>array($excludeID),但我无法捕获第一个循环中的所有id。
以下是我目前的代码:
<?php get_header(); ?>
<section class="content latest">
<h2>Latests 20 posts</h2>
<ul>
<?php $the_query = new WP_Query( \'posts_per_page=20\' ); ?>
<?php $excludeID = array(); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php $excludeID = $post->ID; ?>
<li>
<!-- post thumbnail linking to the single post page -->
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<!-- title -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<!-- date -->
<p class="date">Posted on: <?php the_time( \'j F Y\' ); ?></p>
<!-- display the post excerpt -->
<p><?php the_excerpt(__(\'(more…)\')); ?></p>
</li>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</section>
<h1><?php echo $excludeID ?></h1>
<section class="content category__popular">
<h2>Popular posts</h2>
<ul>
<?php
$args = array(
\'post_type\' => \'post\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'posts_per_page\' => 6,
\'category_name\' => \'popular\',
\'paged\' => get_query_var(\'paged\'),
\'post_parent\' => $parent,
\'post__not_in\' => array($excludeID)
);
$popularquery = new WP_Query($args); ?>
<?php while($popularquery->have_posts()) : $popularquery->the_post(); ?>
<li>
<!-- post thumbnail linking to the single post page -->
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<!-- title -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<!-- date -->
<p class="date">Posted on: <?php the_time( \'j F Y\' ); ?></p>
<!-- display the post excerpt -->
<p><?php the_excerpt(__(\'(more…)\')); ?></p>
</li>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</section>
<?php get_footer(); ?>
最合适的回答,由SO网友:made2popular 整理而成
<?php get_header(); ?>
<section class="content latest">
<h2>Latests 20 posts</h2>
<ul>
<?php $the_query = new WP_Query( \'posts_per_page=20\' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php $excludeID[] = $post->ID; ?>
<li>
<!-- post thumbnail linking to the single post page -->
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<!-- title -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<!-- date -->
<p class="date">Posted on: <?php the_time( \'j F Y\' ); ?></p>
<!-- display the post excerpt -->
<p><?php the_excerpt(__(\'(more…)\')); ?></p>
</li>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</section>
<h1><?php print_r( $excludeID); ?></h1>
<section class="content category__popular">
<h2>Popular posts</h2>
<ul>
<?php
$args = array(
\'post_type\' => \'post\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'posts_per_page\' => 6,
\'category_name\' => \'popular\',
\'paged\' => get_query_var(\'paged\'),
\'post_parent\' => $parent,
\'post__not_in\' => $excludeID
);
$popularquery = new WP_Query($args); ?>
<?php while($popularquery->have_posts()) : $popularquery->the_post(); ?>
<li>
<!-- post thumbnail linking to the single post page -->
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<!-- title -->
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<!-- date -->
<p class="date">Posted on: <?php the_time( \'j F Y\' ); ?></p>
<!-- display the post excerpt -->
<p><?php the_excerpt(__(\'(more…)\')); ?></p>
</li>
<?php endwhile;
wp_reset_postdata();
?>
</ul>
</section>
<?php get_footer(); ?>
因为它是一个数组,所以我使用print\\u r()函数来显示所有ID。我还没有测试第二个循环。请检查它是否对您有效。