post 每页不适用于 wordpress 中的粘性 post 查询
post per page is not working with sticky post query in wordpress
this is my post loop that i am using
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => 1
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile;
wp_reset_postdata();
?>
我尝试使用 offset
但没有成功,
我认为我的循环有问题
如果有人能帮我解决这个问题
提前致谢
您需要的所有信息都可以在theme handbook
中找到
如手抄本所述,这仅显示第一个粘性 post,如果 none return 最后一个 post 发布:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
您的问题可能出在 rsort();
函数上,因为它正在将数组 从最高到最低反转。
试试下面的代码。
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$posts_per_page = 12;
$sticky_count = count($sticky);
if ($sticky_count < $posts_per_page) {
$posts_per_page = $posts_per_page - $sticky_count;
} else {
$posts_per_page = 1;
}
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => $posts_per_page
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile; wp_reset_postdata(); ?>
this is my post loop that i am using
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => 1
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile;
wp_reset_postdata();
?>
我尝试使用 offset
但没有成功,
我认为我的循环有问题
如果有人能帮我解决这个问题 提前致谢
您需要的所有信息都可以在theme handbook
中找到如手抄本所述,这仅显示第一个粘性 post,如果 none return 最后一个 post 发布:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
您的问题可能出在 rsort();
函数上,因为它正在将数组 从最高到最低反转。
试试下面的代码。
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$posts_per_page = 12;
$sticky_count = count($sticky);
if ($sticky_count < $posts_per_page) {
$posts_per_page = $posts_per_page - $sticky_count;
} else {
$posts_per_page = 1;
}
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => $posts_per_page
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<article class="cust-arc-post">
<img src="<?php the_post_thumbnail_url(); ?>" alt="">
<div class="arc-post-header">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<a class="cat" href="javascript:;">Category Title</a>
</div>
<p><?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
</article>
<?php endwhile; wp_reset_postdata(); ?>