0

If I post an article and define several categories, in this case all that are in the loops, the only post that will appear will be the one, because it is the last one.

What I want is that the last post of each category appears without repeating, if the last post is in several categories, then I want it to show the penultimate one.

With $do_not_duplicate[] = $post->ID; does not work

How the posts are appearing:

enter image description here

Code:

<?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; ?>
4
  • 1
    you set $do_not_duplicate but then you're not doing anything with that array to exclude them from the other queries.
    – Milo
    Feb 10 '17 at 18:02
  • what do you suggest? Feb 10 '17 at 18:59
  • codex.wordpress.org/The_Loop
    – Christina
    Feb 10 '17 at 22:30
  • $do_not_duplicate = $post->ID; did not work Feb 10 '17 at 22:57
1

You are adding post IDs to $do_not_duplicate, but you are missing the other half of the process- excluding those post IDs from subsequent queries. You can do that by setting post__not_in with the array of IDs you don't want returned.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.