我已经创建了一个wp\\u查询,在这里我会在每发第四篇文章后更改文章格式。不幸的是,第一篇帖子显示了两次,这肯定是我的模条件出了问题。我感谢你的帮助。非常感谢。
<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 -->
最合适的回答,由SO网友:Sally CJ 整理而成
这个else
的$change % 5 == 0
也在以下情况下运行$change
是一个1
这就是为什么第一篇帖子会显示两次。
你可以通过改变
<?php $change++ ?>
<?php endif; ?>
<?php if ( $change % 5 == 0 ) : ?>
到
<?php $change++ ?>
<?php elseif ( $change % 5 == 0 ) : ?>
或者你也可以试试这个
$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;
一、 e.简单地利用
$myposts
索引。