3
votes

I am cutomizing my WordPress theme and I want to add latest 2 sticky post at the top of my Wordpress home page. For that I use following code:

<div class="trending-right">
   <?php
     $sticky = get_option( 'sticky_posts' ); // Get all sticky posts
     rsort( $sticky ); // Sort the stickies, latest first
     $sticky = array_slice( $sticky, 0, 2 ); // Number of stickies to show
     query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query

     if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
     <div class="trend-post">
     <div class="thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
     <div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
     </div>
     <?php endwhile;?>
     <?php } else { echo ""; }?>

</div>

Now the code works OK and shows the latest 2 sticky posts however it also removes all other listed posts from home page and shows only those 2 sticky posts. I tried replacing query_posts with new WP_Query but in that case it shows ALL sticky posts instead of only 2.

Any suggestion how to tweak above code and make it work?

1

1 Answers

3
votes

Looking at your code, I am assuming you've just shown us the sticky loop and there is another query to display the other posts elsewhere in the template?. You should use wp_reset_query(); after your custom query, here's the entry in the Codex;

Wordpress - resetting custom query