1

So, I have archive.php file with this part of code

<div class="column is-full is-three-quarters p-small">
    <?php
        while ( have_posts() ) : the_post();
            get_template_part( 'loops/loop', 'archive' );
        endwhile;
        else :
            get_template_part( 'template-parts/content', 'none' );
        endif;
    ?>
</div>

in file loop-archive.php I have something like this

<?php
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'cat' => '3'
);
$query = new WP_Query($args);

if ($query->have_posts()) :
while ($query->have_posts()) :
    $query->the_post();

    // set default featured image
    $thumb = get_field('post_thumb');
    $post_thumb = ($thumb ? $thumb['url'] : get_template_directory_uri() . '/dist/images/default-post-featured.jpg');

    ?>
    <div class="columns">
        <div class="column is-one-third">
            <div class="article-thumb">
                <?php echo $post_thumb ?>
            </div>
        </div>
        <div class="column is-two-third">
            <article class="content has-text-left">
                <div class="article-head">
                    <header>
                        <h3><?php echo the_title(); ?></h3>
                        <span>dodano: <?php echo get_the_date('d.m.Y') ?></span>
                    </header>
                </div>
                <div class="article-body">
                    <?php echo the_excerpt(); ?>
                </div>
                <div class="article-foot">
                    <a href="<?php echo the_permalink(); ?>" class="button button__rounded button__gold">Więcej</a>
                </div>
            </article>
        </div>
    </div>
<?php
endwhile;
wp_reset_postdata();
else:
?>

<div class="col">
    <h5>Brak wyników spełniających kryteria wyszukiwania</h5>
</div>

and it works. But it duplicate posts. I have 2 post but loop shows 4 posts. I tried using $do_not_duplicate trick but it don't work for me. What's wrong with my code? How to avoid posts duplicate?

0

OK, so the code does exactly what it is supposed to.

In your archive.php you do this:

while ( have_posts() ) : the_post();
    get_template_part( 'loops/loop', 'archive' );

So for every post (both of them) you include loops/loop-archive.php file. And inside that file you don't display only the current post from global loop, but you make your own, custom loop. And this one again iterates through all posts (because you set pre_get_posts to -1.

So your code, after some simplifications looks like this:

while ( have_posts() ) : the_post();
    // the part from get_template_part( 'loops/loop', 'archive' );
    $query = new WP_Query($args);

    if ($query->have_posts()) :
        while ($query->have_posts()) :
            $query->the_post();
            // display the post

So yes - it shows 4 posts instead of 2. And it will display 9 posts instead of 3, if there will be 3 posts - because for every post you display all posts.

  • Damn, I had one unclosed if before code I've posted. So, now it's working like it should! Thank you (dzięki wielkie) ;) – MMPL1 Feb 6 at 16:47

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.