我需要在主页上显示所有会有所不同的帖子。也就是说, 它们不会在同一页面上重复。我使用WP_Query显示它们, 但是对于帖子的每一列, 我都需要有另一个”
posts_per_page”
。怎么做?
我尝试这样做:
<
?php $my_query = new WP_Query($args);
?>
【如何不在同一页面上重复帖子(使用WP_Query吗?)】和
this <
?php$my_query->
set('posts_per_page', 3);
$my_query->
query($my_query->
query_vars);
?>
这是我的代码示例一:
<
div class="row">
<
div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
<
?php$my_query->
set('posts_per_page', 3);
$my_query->
query($my_query->
query_vars);
?>
<
?php while ( $my_query->
have_posts() ) : $my_query->
the_post();
?>
<
article class="post post-id-<
?php echo the_ID();
?>
">
<
a class="post-thumbnail" href="http://www.srcmini.com/<
?php the_permalink() ?>" title="<
?php the_title();
?>
">
<
?php the_post_thumbnail();
?>
<
/a>
<
div class="post-content">
<
h3 class="post-title">
<
a href="http://www.srcmini.com/<
?php the_permalink() ?>">
<
?php the_title();
?>
<
/a>
<
/h3>
<
?php healthybroom_posted_on();
?>
<
?php healthybroom_entry_footer();
?>
<
p class="post-excerpt">
<
?php echo get_the_excerpt();
?>
<
/p>
<
/div>
<
/article>
<
?php endwhile;
?>
<
/div>
<
div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
<
?php$my_query->
set('posts_per_page', 5);
$my_query->
query($my_query->
query_vars);
?>
<
?php while ( $my_query->
have_posts() ) : $my_query->
the_post();
?>
<
article class="post post-id-<
?php echo the_ID();
?>
">
<
a class="post-thumbnail" href="http://www.srcmini.com/<
?php the_permalink() ?>" title="<
?php the_title();
?>
">
<
?php the_post_thumbnail();
?>
<
/a>
<
div class="post-content">
<
?php healthybroom_entry_footer();
?>
<
h3 class="post-title">
<
a href="http://www.srcmini.com/<
?php the_permalink() ?>">
<
?php the_title();
?>
<
/a>
<
/h3>
<
?php healthybroom_posted_on();
?>
<
/div>
<
/article>
<
?php endwhile;
?>
<
/div>
<
/div>
我希望不要简单重复这些帖子。
#1如果你有2个带有随机帖子的循环, 并且希望避免在2个循环中有相同的帖子, 则可以使用以下代码:
<
!-- We create an array for saving the IDs of the post displaying in the first loop -->
<
?php $store_posts_ids = array();
?>
<
div class="row">
<
div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
<
?php
$my_query->
set('posts_per_page', 3);
$my_query->
query($my_query->
query_vars);
?>
<
?php while ( $my_query->
have_posts() ) : $my_query->
the_post();
?>
<
!-- We store the post id for the second loop -->
<
?php array_push( $store_posts_ids, get_the_ID() );
?>
<
!-- Your code here -->
<
?php endwhile;
?>
<
/div>
<
div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
<
?php
/*
If you want to display 5 posts, you need to have a loop
of 7 posts because you have 3 posts in your first loop
*/
$number_post = 0;
$my_query->
set('posts_per_page', 5);
$my_query->
query($my_query->
query_vars);
?>
<
?php while ( $my_query->
have_posts() ) : $my_query->
the_post();
?>
<
?php array( $store_posts_ids, get_the_ID() );
?>
<
!-- If we have our 5 posts, no need to display more posts -->
<
?php if( $number_post <
5 ): ?>
<
!-- If the post is not in the first loop -->
<
?php if( !in_array( $store_posts_ids, get_the_ID() ) ): ?>
<
?php $number_post = $number_post + 1;
?>
<
!-- Your code here -->
<
?php endif;
?>
<
?php endif;
?>
<
?php endwhile;
?>
<
/div>
<
/div>
推荐阅读
- 如何通过点击锚链接打开子菜单()
- 如何将在父主题中进行的所有修改移到子主题()
- 如何在WordPress中合并2个列表菜单
- 如何使主题看起来像屏幕截图()
- Beyond Compare隐藏文本细节小技巧
- Beyond Compare与输出文件比较办法
- p2p种子搜索器超凡搜索怎样搜资源种子?
- Beyond Compare快捷键设置办法
- p2p种子搜索器迅雷资源助手运用图文详细教程