Show latest 2 sticky posts at WordPress home page

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?

Answers


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


Need Your Help

How can I avoid a “jump” while scaling on a canvas, when the scaling center changes due to a pinching (zoom) or other gesture in a metro-style app

c# microsoft-metro windows-runtime

How can I avoid a "jump" while scaling on a canvas object, when the scaling center changes due to a pinching (zoom) or other gesture in a metro-style app

Validating Timestamp format yyyy-MM-dd'T'HH:mm:ssZ in java?

java date jodatime

I'm trying to do a timestamp validation using joda time-1.6.2. Please point my error and help me out.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.