使用不同的偏移量查询不同循环中的不同类别

时间:2021-01-19 17:13:05

标签: wordpress loops foreach offset

我有四个循环。在第一个循环中,我想显示所有类别的所有当前文章(突发新闻)。在第二个循环中,只应显示类别为 4、5、6、7 的文章。但是,如果在第 1 次循环中发表了这些类别的文章,则这篇最新文章不应出现在第 2 次循环中。到目前为止,我只阅读了文章的一般排除,而没有阅读条件。

1 个答案:

答案 0 :(得分:0)

您必须使用 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();
相关问题