WordPress 置顶帖子和 "posts_per_page" 令人困惑

Wordpress sticky posts and "posts_per_page" confusing

我有一个带有此参数的自定义循环:

$sticky = count(get_option('sticky_posts'));
$main_loop = array (
    'posts_per_page' => 4 - $sticky
);

我想做以下事情:

但我不会让它工作。目前我有以下情况:

简而言之: 当我从最近的 3 post 中坚持 post 时,它不起作用,当我坚持超过 3 posts 它不工作。

这是完整的循环:

<section>
<h2>Aktuelles</h2>
<?php
$sticky = count(get_option('sticky_posts'));
// WP_Query arguments
$main_loop = array (
'posts_per_page' => 4 - $sticky
);

// The Query
$query = new WP_Query( $main_loop );

// The Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>

<?php endwhile; wp_reset_postdata(); ?>
</section>

因为我找不到这个问题的答案,所以我使用了另一种方法,结合了 2 个循环,总是显示 4 posts。诀窍是从第二个循环中减去粘性 post 计数。

<?php 
    $sticky = count(get_option('sticky_posts'));
    if ($sticky > 0) { 
?>
<?php   
    // First loop with sticky posts
    $main_loop_s = array (
        'posts_per_page'         => $sticky,
        'post__in' => get_option('sticky_posts'),
    );
    // The Query
    $do_not_duplicate = array();
    $query = new WP_Query( $main_loop_s );
    // The Loop
    while ( $query->have_posts() ) : $query->the_post(); $do_not_duplicate[] = $post->ID; ?>
    <h3><?php the_title(); ?></h3>      
    <?php endwhile;
?>
<?php // stickycheck end 
    } 
?>
<?php 
    $sticky = count(get_option('sticky_posts'));
    if ($sticky < 4) { 
?>
<?php
    $allstickys = 4 - $sticky;
    // Second loop with rest of posts up to 4
    $main_loop_ns = array (
        'posts_per_page'         => $allstickys,
        'offset'                 => $sticky,
        'post__not_in'           => $do_not_duplicate
    ); 
    // The Query
    $query = new WP_Query( $main_loop_ns );
    // The Loop
    while ( $query->have_posts() ) : $query->the_post(); ?>
        <h3><?php the_title(); ?></h3>          
    <?php endwhile; wp_reset_postdata();
?>
<?php // stickycheck end 
    } 
?>