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
);
我想做以下事情:
- 1 sticky post: 我想显示 3 posts + sticky post
- 2 sticky posts: 我想显示 2 posts + 2 sticky posts
- 3 sticky posts: 我想显示 1 posts + 3 sticky posts
- 4 sticky posts: 我只想显示 4 sticky posts
但我不会让它工作。目前我有以下情况:
- 1 sticky post from 3 latest posts (1,2 or 3): 我有一个 sticky post + 2 posts
- 1 个 sticky post 来自第四个(或更早的)posts (4,5,6 ...):我有 1 个 sticky post + 3 posts(就像我想要的那样)
- 2 sticky posts 从第四个(或更老的)posts (4,5,6 ...): 我有 2 sticky post + 2 posts(如我所愿)
- 3 sticky posts 从第四个(或更老的)posts (4,5,6 ...): 我有 3 sticky post + 1 post(如我所愿)
- 4 个 sticky posts 来自第四个(或更早的)posts (4,5,6 ...):我有 4 个 sticky post + 3 posts(但我想显示最多 4 posts)
简而言之: 当我从最近的 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
}
?>
我有一个带有此参数的自定义循环:
$sticky = count(get_option('sticky_posts'));
$main_loop = array (
'posts_per_page' => 4 - $sticky
);
我想做以下事情:
- 1 sticky post: 我想显示 3 posts + sticky post
- 2 sticky posts: 我想显示 2 posts + 2 sticky posts
- 3 sticky posts: 我想显示 1 posts + 3 sticky posts
- 4 sticky posts: 我只想显示 4 sticky posts
但我不会让它工作。目前我有以下情况:
- 1 sticky post from 3 latest posts (1,2 or 3): 我有一个 sticky post + 2 posts
- 1 个 sticky post 来自第四个(或更早的)posts (4,5,6 ...):我有 1 个 sticky post + 3 posts(就像我想要的那样)
- 2 sticky posts 从第四个(或更老的)posts (4,5,6 ...): 我有 2 sticky post + 2 posts(如我所愿)
- 3 sticky posts 从第四个(或更老的)posts (4,5,6 ...): 我有 3 sticky post + 1 post(如我所愿)
- 4 个 sticky posts 来自第四个(或更早的)posts (4,5,6 ...):我有 4 个 sticky post + 3 posts(但我想显示最多 4 posts)
简而言之: 当我从最近的 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
}
?>