最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - First post in loop displays twice

programmeradmin1浏览0评论

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 -->

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 -->
Share Improve this question asked Apr 22, 2019 at 0:22 user5854648user5854648 16317 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论