How to display sticky or latest published post on the current category page it belongs to?
What I'm able to do :
When there is a sticky post in the current category, I'm already able to display it. The problem occurs when there is no sticky post : it doesn't display the latest post as it should do (actually id doesn't display anything).
It works like a charm on the Home page : when there is a sticky post, it displays it, and where there is no sticky post, it displays the latest post.
Here is my code :
?php
$current_object = get_queried_object_id(); // Get the category ID and store it in a variable
// Display sticky post or latest post :
$args = array(
'posts_per_page' = 1,
'post__in' = get_option( 'sticky_posts' ),
'cat' = $current_object, // Current category ID
'ignore_sticky_posts' = true,
);
$featured_query = new WP_query( $args );
if ( $featured_query-have_posts() ) : ?
div class=featured-post
?php
while ( $featured_query-have_posts() ) :
$featured_query-the_post();
the_title('h2', '/h2');
the_excerpt();
endwhile;
wp_reset_postdata();
?
/div
?php endif; ?
What I want to achieve :
I want my category page to display the sticky post that belongs to this category if it exists, and if not, I want that the category page displays the latest post published in this category.
What am I missing ?
Topic sticky-post categories Wordpress
Category Web