admin管理员组文章数量:1276756
In the category post archive, I want to display the last 10 posts and 1 featured post (featured post only on the first page). I can choose a featured post that will be displayed first (user can select manually, its custom field is called ‘category_featured_post’ ). Now if the featured post is the same post that will be displayed in a list, It will be doubled (on featured position and on the list). I can hide this post on the list (if it is displayed on the first page where the featured post is displayed), but now there will be 9 posts on the list instead of 10. How can I display 10 posts in that case?
Custom field category_featured_post
is on category, it is a Post Object that returns a single post ID.
Here is the code I have now:
<!-- DISPLAY FEATURED POST (ONLY ON FIRST PAGE) IN BIG TILE -->
<?php
if ( !is_paged() ) :
$post = get_post($featured_post);
?>
<div class="tile">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<a href="<?php echo the_permalink(); ?>">Read post</a>
</div>
<?php
endif;
?>
<!-- DISPLAY POSTS IN LIST -->
<?php
if (have_posts()){
while(have_posts()){
the_post();
// DON'T DISPLAY POST IF IT'S FEATURED
if(get_the_ID() != $featured_post ){
get_template_part('template-parts/post/content','archive');
}
}
}
?>
In the category post archive, I want to display the last 10 posts and 1 featured post (featured post only on the first page). I can choose a featured post that will be displayed first (user can select manually, its custom field is called ‘category_featured_post’ ). Now if the featured post is the same post that will be displayed in a list, It will be doubled (on featured position and on the list). I can hide this post on the list (if it is displayed on the first page where the featured post is displayed), but now there will be 9 posts on the list instead of 10. How can I display 10 posts in that case?
Custom field category_featured_post
is on category, it is a Post Object that returns a single post ID.
Here is the code I have now:
<!-- DISPLAY FEATURED POST (ONLY ON FIRST PAGE) IN BIG TILE -->
<?php
if ( !is_paged() ) :
$post = get_post($featured_post);
?>
<div class="tile">
<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<a href="<?php echo the_permalink(); ?>">Read post</a>
</div>
<?php
endif;
?>
<!-- DISPLAY POSTS IN LIST -->
<?php
if (have_posts()){
while(have_posts()){
the_post();
// DON'T DISPLAY POST IF IT'S FEATURED
if(get_the_ID() != $featured_post ){
get_template_part('template-parts/post/content','archive');
}
}
}
?>
Share
Improve this question
edited Nov 2, 2021 at 11:01
mikolaj
asked Nov 1, 2021 at 18:49
mikolajmikolaj
12 bronze badges
10
|
Show 5 more comments
1 Answer
Reset to default 0Thanks to liquidRock and StudioAl for solution!
Exclude post from query:
'post__not_in' => array( $featured_post_id )
本文标签: functionsExclude specific post from query
版权声明:本文标题:functions - Exclude specific post from query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741239816a2363750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
archive.php
is loaded the query has already ran, so changing its parameters will not change the number of posts because it's already fetched the posts, it's too late. Making it work this way would mean discarding the query and putting a new one in ( very bad for performance, will introduce lots of pagination issues ), or it would require time travelling into the past several micro-seconds. Additionally, post archives don't have an ID because they aren't posts/pages, they're archives – Tom J Nowell ♦ Commented Nov 1, 2021 at 19:17archive.php
covers all archives, date archives, tag archives etc etc, can you be more specific? – Tom J Nowell ♦ Commented Nov 1, 2021 at 21:19category_featured_post
is on category, it is a Post Object that returns a single post ID. I'm talking about posts archive. – mikolaj Commented Nov 1, 2021 at 21:24