-1

I have created a wp_query where I change the post format after every 4th post. Unfortunately, the first post is displaying twice which is surely something wrong with my modulo conditions. I appreciate any help. Thank you.

<div class="container-fluid">
    <?php
        $category = get_category( get_query_var( 'cat' ) );
        $cat_id = $category->cat_ID;
        $change = 0;
        $myposts = get_posts(array('posts_per_page' => 1000000, 'offset' => 4, 'category__in' => array($cat_id), 'post_status'=>'publish', 'order'=>'DESC' ));
        foreach($myposts as $post) : setup_postdata($post);
    ?>

    <?php if ( $change == 0 ) : ?>
    <div class="row mx-auto align-items-center">  
        <div class="col-sm-5">
            <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
            <a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>"></a>
        </div>

        <div class="col-sm-7 mx-auto card-block-txt">
            <h3><a class="align-middle" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        </div>
    </div><!-- END ROW -->
    <?php $change++ ?>
    <?php endif; ?>

    <?php if ( $change % 5 == 0 ) : ?>
    <div class="row mx-auto align-items-center"> 
        <div class="col-sm-12">
          <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
          <a href="<?php the_permalink(); ?>">
            <img src="<?php echo $image[0]; ?>" class="card-img img-fluid hv-img">
          </a>
        </div>

        <div class="col-sm-12">
          <?php
            $categories = get_the_category();
            if ( $categories ) : $deepChild = get_deep_child_category( $categories );
          ?>
            <?php endif; ?>
            <h3><?php the_title(); ?></h3>
        </div>
    </div><!-- END ROW -->

    <?php else : ?>
    <div class="row mx-auto align-items-center"> 
        <div class="col-sm-5">
            <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
            <a href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>"></a>
        </div>

        <div class="col-sm-7 mx-auto">
            <h3 class="front-post-sub align-middle"><a class="align-middle" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        </div>
    </div>

    <?php endif;?>
    <?php $change++ ?>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    <?php get_footer(); ?>
</div><!-- END CONTAINER -->
1

The else of the $change % 5 == 0 also runs when $change is a 1 and that is why the first post is displayed twice.

You could fix it by changing

<?php $change++ ?>
<?php endif; ?>

<?php if ( $change % 5 == 0 ) : ?>

to

<?php $change++ ?>
<?php elseif ( $change % 5 == 0 ) : ?>

Or you might also try this one...

$myposts = get_posts(array(...));
foreach($myposts as $change => $post) : setup_postdata($post);
    if ( $change == 0 ) : ?>
        The first post.
        <h3><?php echo 'Post #' . ( $change + 1 ) . ': ' . $post->post_title; ?></h3>
    <?php
    elseif ( ( $change + 1 ) % 5 == 0 ) : ?>
        After every 4th post.
        <h3><?php echo 'Post #' . ( $change + 1 ) . ': ' . $post->post_title; ?></h3>
    <?php
    else : ?>
        All other posts.
        <h3><?php echo 'Post #' . ( $change + 1 ) . ': ' . $post->post_title; ?></h3>
    <?php
    endif;
endforeach;

I.e. Simply make use of the $myposts indexes.

  • 1
    Thank you, I actually didn't realize why when I tried elseif in the past it wouldn't work and it's because I would always end the if. Makes so much sense seeing it like this. – user5854648 Apr 22 '19 at 2:51
  • 1
    I updated the other version. It might help you or someone else. :) – Sally CJ Apr 22 '19 at 5:00

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.