我有一个带有这个args的自定义循环:
$sticky = count(get_option('sticky_posts'));
$main_loop = array (
'posts_per_page' => 4 - $sticky
);
我想做以下事情:
但我不能让它发挥作用。目前我有以下情况:
简而言之:当我从最新的3个帖子中贴出帖子时,它无法正常工作,当我发布超过3个帖子时,它无效。
这是完整循环:
<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>
答案 0 :(得分:1)
由于我没有找到这个问题的答案,我使用另一种方法,结合了2个循环,总是显示4个帖子。诀窍是从第二个循环中减去粘性帖子数。
<?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
}
?>