星期三 , 22 1 月 2025

wp置顶文章只在第一页第二页还输出置顶文章的解决方案

wp默认的置顶文章只在第一页置顶,超过第一页的话置顶文章依然还会存在他原来的位置上,要解决这种方法,让置顶文章一直都消失在第一页置顶的话,参考下面的代码进行输出: 或者直接用两个wp query分别输出

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// Fetch sticky posts separately
$sticky = get_option('sticky_posts');
$args = array(
    'post__in' => $sticky,
    'ignore_sticky_posts' => 1,
    'paged' => $paged
);

$sticky_query = new WP_Query($args);

// Fetch regular posts for the current page
$args = array(
    'post__not_in' => $sticky,
    'paged' => $paged
);

$regular_query = new WP_Query($args);

// Merge both queries
$merged_query = new WP_Query();
$merged_query->posts = array_merge($sticky_query->posts, $regular_query->posts);
$merged_query->post_count = $sticky_query->post_count + $regular_query->post_count;

// Display the posts
if ($merged_query->have_posts()) {
    while ($merged_query->have_posts()) {
        $merged_query->the_post();
        // Display post content as desired
    }
}

Check Also

Canva slider

https://www.can …

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注