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');
}
}
}
?>
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 archivesarchive.php
covers all archives, date archives, tag archives etc etc, can you be more specific?category_featured_post
is on category, it is a Post Object that returns a single post ID. I'm talking about posts archive.