【发布时间】:2021-01-19 17:13:05
【问题描述】:
我有四个循环。在第一个循环中,我想显示所有类别的所有当前文章(突发新闻)。在第二个循环中,仅应显示类别为 4、5、6、7 的文章。但是,如果在第 1 个循环中发布了来自这些类别的文章,则该最新文章不应出现在第 2 个循环中。到目前为止,我只阅读了有关文章的一般排除,但没有阅读有关条件的内容。
【问题讨论】:
标签: wordpress loops foreach offset
我有四个循环。在第一个循环中,我想显示所有类别的所有当前文章(突发新闻)。在第二个循环中,仅应显示类别为 4、5、6、7 的文章。但是,如果在第 1 个循环中发布了来自这些类别的文章,则该最新文章不应出现在第 2 个循环中。到目前为止,我只阅读了有关文章的一般排除,但没有阅读有关条件的内容。
【问题讨论】:
标签: wordpress loops foreach offset
你必须使用category__not_in:
$include_cats = array( 4, 5, 6, 7 );
$exclude_cats = array( 3 ); // 'breaking news' TERM ID
$posts_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4 ,
'ignore_sticky_posts' => true,
'category__in' => $include_cats,
'category__not_in' => $exclude_cats,
'orderby' => 'date',
);
$posts = new WP_Query( $posts_args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
?>
<a href="<?php echo get_the_permalink( $posts->post->ID ); ?>" title="<?php echo get_the_title( $posts->post->ID ); ?>">
<?php
if ( has_post_thumbnail( $posts->post->ID ) ) {
echo get_the_post_thumbnail( $posts->post->ID, $img, array( 'class' => 'post-thumb' ) ); // YOUR IMAGE SIZE
} else {
echo '<img class="' . $img . '" src="placeholder.png" width="150" height="150" alt="' . get_the_title( $posts->post->ID ) . '">'; //ADD PLACEHOLDER IMAGE HERE
}
?>
<h3 class="post-title"><?php echo get_the_title( $posts->post->ID ); ?></h3>
<span class="author"><?php echo get_the_author( $posts->post->ID ); ?></span>
</a>
<?php
}
}
wp_reset_postdata();
【讨论】:
<h2>Loop n°1</h2> <?php $ids = array(); while (have_posts()) : the_post(); the_title(); ?> <?php $ids[]= $post->ID; endwhile; ?> <h2>Loop n°2</h2> <?php query_posts("showposts=50"); while (have_posts()) : the_post(); if (!in_array($post->ID, $ids)) { the_title();?> <?php } endwhile; ?> 从这里:[链接] (isitwp.com/avoid-duplicate-posts-in-multiple-loops)