Skip to content

Instantly share code, notes, and snippets.

@juliuzfan
Created November 28, 2011 00:56
Show Gist options
  • Save juliuzfan/1398628 to your computer and use it in GitHub Desktop.
Save juliuzfan/1398628 to your computer and use it in GitHub Desktop.
Custom WP Query For Sticky Posts Only
<?php
/**
Custom Query for Sticky Posts only
----------------------------------
*/
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 2, // Number of sticky posts to get
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
if ( !empty($sticky) ):
// has sticky posts
query_posts($args);
$stickyPosts = new WP_query();
$stickyPosts->query($args);
if ( $stickyPosts->have_posts() ):
?>
<!-- Sticky Posts Section -->
<div class="sticky-posts clearfix">
<!-- Intro -->
<div class="section-intro"><p><strong class="section-title">Sticky Articles Section</strong> <span>&bull;</span> <em class="section-description">Some short section description</em></p></div>
<?php
while ( $stickyPosts->have_posts() ) : $stickyPosts->the_post();
?>
<article>
<div class="entry-meta clearfix"><span class="author"><?php _e('by: '); the_author_posts_link(); ?></span> <span class="topic"><?php the_category( ', ' ); ?></span></div>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<div class="excerpt">
<figure class="alignleft">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<figcaption><?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?></figcaption>
</figure>
<?php the_excerpt();?>
<p><a href="<?php the_permalink()?>" title="<?php the_title_attribute( array( 'before' => __('Continue reading: '), 'after' => ' &rarr;' ) ); ?>"> <?php _e( 'Continue Reading <span class="meta-nav">&rarr;</span>'); ?></a></p>
</div>
</article>
<?php
endwhile; //end loop for sticky posts
endif; //have_posts()
?>
</div> <!-- end .sticky-posts -->
<?php
// RESET THE QUERY
wp_reset_query();
endif; //has sticky posts
//END Custom Query for Sticky Posts only
@dashing-coder
Copy link

Thank you for the post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment