不要在多个循环中重复多个类别的帖子

时间:2017-02-10 作者:Gabriel Souza

如果我发布了一篇文章并定义了几个类别,在这种情况下,循环中的所有内容都会出现,唯一会出现的帖子就是那篇,因为它是最后一篇。

我想要的是,每个类别的最后一篇文章都不会重复出现,如果最后一篇文章分为几个类别,那么我希望它显示倒数第二个类别。

使用$do\\U not\\U duplicate[]=$post->ID;不起作用

帖子的显示方式:

enter image description here

代码:

<?php $page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'page\' => $page, \'cat\' => \'cat=2 \' );
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;  ?>
                <div class="intro-noticia-1">
                    <a href="<?php the_permalink(); ?>">
                <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\' );?>
                <div class="intro-post-thumbnail" style="background: url(\'<?php echo $backgroundImg[0]; ?>\'); background-size: cover; background-position: center;">
                </div></a>
                </div>
        <?php endwhile; ?>

                <?php $page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'page\' => $page, \'cat\' => \'cat=3 \' );
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;  ?>
                <div class="intro-noticia-2">
                    <a href="<?php the_permalink(); ?>">
                <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\' );?>
                <div class="intro-post-thumbnail" style="background: url(\'<?php echo $backgroundImg[0]; ?>\'); background-size: cover; background-position: center;">
                </div></a>
                </div>
        <?php endwhile; ?>

                <?php $page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'page\' => $page, \'cat\' => \'cat=4 \' );
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;  ?>
                <div class="intro-noticia-3">
                    <a href="<?php the_permalink(); ?>">
                <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\' );?>
                <div class="intro-post-thumbnail" style="background: url(\'<?php echo $backgroundImg[0]; ?>\'); background-size: cover; background-position: center;">
                </div></a>
                </div>
        <?php endwhile; ?>

                <?php $page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'page\' => $page, \'cat\' => \'cat=5 \' );
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;  ?>
                <div class="intro-noticia-4">
                    <a href="<?php the_permalink(); ?>">
                <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\' );?>
                <div class="intro-post-thumbnail" style="background: url(\'<?php echo $backgroundImg[0]; ?>\'); background-size: cover; background-position: center;">
                </div></a>
                </div>
        <?php endwhile; ?>

                <?php $page = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        $args = array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'page\' => $page, \'cat\' => \'cat=6 \' );
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID;  ?>
                <div class="intro-noticia-5">
                    <a href="<?php the_permalink(); ?>">
                <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'large\' );?>
                <div class="intro-post-thumbnail" style="background: url(\'<?php echo $backgroundImg[0]; ?>\'); background-size: cover; background-position: center;">
                </div></a>
            </div>
        <?php endwhile; ?>

1 个回复
最合适的回答,由SO网友:Milo 整理而成

您正在将帖子ID添加到$do_not_duplicate, 但是,您缺少了该过程的另一半—将这些post ID从后续查询中排除。您可以通过设置post__not_in 使用您不希望返回的ID数组。

$args = array(
    \'posts_per_page\' => 1,
    \'paged\' => $page,
    \'cat\' => 2,
    \'post__not_in\' => $do_not_duplicate,
);

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x