How not to repeat Wordpress loop posts

Asked

Viewed 228 times

1

I’m creating a gallery of post like that of globe with. that appear 3 different posts at once.

The problem is that it only picks up a post, I would like the *posts to be inserted within the div without repeating.

<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 are you getting:

Imagem mostrando o resultado do código acima

1 answer

4


You don’t have to make a while, you can just pick up 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; ?>
  • face this and very simple however important mt, solved a bunch of gambiarra that I had done, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.