How not to repeat WordPress loop posts

0

I'm creating a post gallery like that of globo.com where 3 different posts appear at once.

The problem is that it only takes a post, I would like * posts to be inserted within div without being repeated.

<div class="row">

<?php   $do_not_duplicate = array(); ?>

<?php   
 $args = array( 'post_type' => 'post',   'posts_per_page' => 1,  'cat' => 1);
$mosaics = new WP_Query( $args ); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>


<div class="img1">


 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">   <?php the_post_thumbnail('class => img-responsive'); ?>  </a>


</div>
<?php   endwhile; wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>

</div>


<div class="row">

<?php   $do_not_duplicate = array(); ?>
<?php   
 $args = array( 'post_type' => 'post',   'posts_per_page' => 1,  'cat' => 1);
$mosaics = new WP_Query( $args); ?>
<?php   while ($mosaics->have_posts()) : $mosaics->the_post(); ?>
<?php   $do_not_duplicate[] = $post->ID; ?>

<div class="img2 col-md-12">

 <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">   <?php the_post_thumbnail('class => img-responsive'); ?>  </a>

</div>


<?php   endwhile; wp_reset_postdata(); ?>

</div>

How's it going:

    
asked by anonymous 05.03.2018 / 02:50

1 answer

2

You do not need to while , you can just get 3 posts from a finished category, and then just use $posts->the_post(); to capture the next, for example:

<?php

$mosaics = new WP_Query([
    'post_type' => 'post', // Tipo da publicação
    'posts_per_page' => 3, // Número de POST
    'cat' => 1             // ID da categoria
]);

if ($mosaics->have_posts()): $mosaics->the_post();
?>
    <div class="row">
        <div class="img1">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail('post-thumbnail', ['class' => 'img-responsive']); ?>
            </a>
        </div>
    </div>

    <?php $mosaics->the_post(); ?>

    <div class="row">
        <div class="img2 col-md-12">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail('post-thumbnail', ['class' => 'img-responsive']); ?>
            </a>
        </div>
    </div>
<?php endif; ?>
    
05.03.2018 / 23:30