admin管理员组文章数量:1418344
I used Justin Tadlock's tutorial on how to create a loop consisting only sticky posts.
The code looked more or less like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 2 );
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
According to the tutorial, I simply need to change the last number from the 3rd line to configure how many posts I wanted to display in the loop.
But I think the code above won't let me display number of posts any bigger than what's stored in posts_per_page
setting (dashboard -> Settings -> Reading)
So here's the question:
How can I make the query above so that it could display more posts than whatever value the posts_per_page
option have?
I used Justin Tadlock's tutorial on how to create a loop consisting only sticky posts.
The code looked more or less like this:
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 2 );
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
According to the tutorial, I simply need to change the last number from the 3rd line to configure how many posts I wanted to display in the loop.
But I think the code above won't let me display number of posts any bigger than what's stored in posts_per_page
setting (dashboard -> Settings -> Reading)
So here's the question:
How can I make the query above so that it could display more posts than whatever value the posts_per_page
option have?
- it seems I just found the answer... I simply need to insert posts_per_page into the query_posts() – Leader Commented Jan 17, 2015 at 13:24
1 Answer
Reset to default 3The issue is that you are using the function query_posts
this only queries the default result on that page. It's advised that you use wp_query
instead, it just eliminates the margin for error or unexpected results.
You can create a new query like below, and specify explicitly how many posts to return:
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post__in' => $sticky,
'posts_per_page' => 10
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
// Do stuff with sticky posts
endwhile;
wp_reset_postdata();
This method also allows you to drop the array_slice
process. So you can simply change posts_per_page to a number of your choice
本文标签: Loop for sticky posts
版权声明:本文标题:Loop for sticky posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745287538a2651588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论