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?

Share Improve this question asked Jan 17, 2015 at 13:19 LeaderLeader 134 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 3

The 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