Last update at :2024-07-06,Edit by888u
I recently discovered that when the WordPress category page calls a pinned article, if there is no pinned article, it will automatically call a normal article. My code for calling the pinned article is as follows:
$cat,
'post__in' => $sticky_posts,
'posts_per_page' => 2,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
?>
...
After using the echo method, I found that when there is no sticky article, the post__in condition is automatically ignored, so I came up with a temporary solution, which is to judge that $sticky_posts is empty and add a 0 to the array, so that post__in will Generate a query id in (0), so it cannot be queried.
if(empty($sticky_posts)){
$sticky_posts = [0];
}
$args = array(
'cat'=> $cat,
'post__in' => $sticky_posts,
'posts_per_page' => 2,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
?>
...
Recommended site search: free IP address in mainland China, US vps server, instant IP server replacement, cheap domain name registration, domain name registration number query, org domain name, query IP address, virtual host space, 1g US virtual host, how to bind the server Defined domain name,
发表评论