Best就是一个例子:
首页。我想在顶部有一个特色部分。如果我的特写中有一篇科学文章,我不希望它也显示在同一页的我的科学部分。
我能不能说点什么?例如:“如果\\u帖子已经出现,不要列出”?
我想将此添加到另一个类别的显示中,作为该页面上的部分,因此一篇分类为科学和健康的文章只显示在其中一个类别上。
谢谢你的建议,如果你有这个建议的话。
J
Best就是一个例子:
首页。我想在顶部有一个特色部分。如果我的特写中有一篇科学文章,我不希望它也显示在同一页的我的科学部分。
我能不能说点什么?例如:“如果\\u帖子已经出现,不要列出”?
我想将此添加到另一个类别的显示中,作为该页面上的部分,因此一篇分类为科学和健康的文章只显示在其中一个类别上。
谢谢你的建议,如果你有这个建议的话。
J
您只需从循环中构建一个帖子ID数组,然后从以下查询集中排除这些帖子
我不知道您的确切结构,但这里有一个非常基本的想法(需要PHP5.4+,因为数组语法很短([]
), 对于旧版本,请更改回旧的数组语法(array()
))
// Define the variable to hold posts
$remove_duplicates = [];
// define and run our first query
$args1 = [
// All your arguments for this query
];
$loop1 = new WP_Query( $args1 );
if ( $loop1->have_posts() ) {
while ( $loop1->have_posts() ) {
$loop1->the_post();
// Build our array of ID\'s to exclude from other query
$remove_duplicates[] = get_the_ID();
// Rest of your loop
}
wp_reset_postdata(); // VERY VERY IMPORTANT
}
// Define our second query and exclude posts from the first
$args2 = [
\'post__not_in\' => $remove_duplicates, // Remove posts from from $loop1
// All your arguments for this query
];
$loop2 = new WP_Query( $args2 );
if ( $loop2->have_posts() ) {
while ( $loop2->have_posts() ) {
$loop2->the_post();
// Rest of your loop
}
wp_reset_postdata(); // VERY VERY IMPORTANT
}