elegant themes

Display sticky post and exclude it from recent posts list in WordPress

| January 11, 2010 | 7 Comments

We were working on a WordPress site that needed to display a sticky post at the top of the blog page with one style, and then below it a list of the most recent posts with a completely different style.

I realize that you can just style the sticky post using the sticky-related classes provided by WordPress:

<div <?php post_class(); ?>> and .sticky class

but I needed to have design elements appear in between the sticky post and the rest of the recent posts, and the recent posts needed to have a completely different design than the sticky. Therefore I decided to go with a 2 loop solution. Once we were using two loops, we had to also make sure that sticky post did not repeat itself and appear in the list of recent posts following it.

Nathan Rice provides a lot of useful information about sticky posts but I couldn’t get the code he posted to work properly. So I went on a hunt to figure it out.

How to display a sticky post

The best solution I found for how to display a WordPress sticky post was a comment by Eduardo on Justin Tadlock’s post Get the Latest Sticky Post. He gave the following code:

<?php
$sticky = get_option( ‘sticky_posts’ );
query_posts( array( ‘post__in’ => $sticky, ‘caller_get_posts’ => 1, ‘orderby’ => ID, ‘showposts’ => 2 ) );
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”>
<?php endwhile;?>

<?php endif; ?>

It worked very nicely.

How to exclude sticky posts from a list of recent posts

I then wanted to show a list of 3 recent posts that excluded the post that had been defined as sticky. After many trials and errors with different solutions posted around the web, I finally found one that worked on the WordPress Support Forum.

<?php $sticky = get_option(‘sticky_posts’) ;

$post_to_exclude[] = $sticky[0];

$args=array(
‘caller_get_posts’=>1,
‘showposts’=>2,
‘post__not_in’=> $post_to_exclude,
);

query_posts($args); ?>

<h2><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a> </h2>

<?php while (have_posts()) : the_post();  ?>

<?php endwhile;?>

So there you have it: a workable way to have a WordPress sticky post with one style, and a list of recent posts excluding the sticky post with a completely different style below the sticky post.

Tags: , , ,

Category: Code Snippets

About Rebecca Markowitz: Rebecca Markowitz has built over 50 WordPress sites for clients as Web Project Manager and WordPress specialist at illuminea. illuminea is a Jerusalem-based boutique web agency. WPGarage shows my dedicated relationship with WordPress over the years - full of love, laughs, tears, growth and strong drinks. L'chaim! View author profile.


Comments (7)

Trackback URL | Comments RSS Feed

  1. md says:

    Thank you so much!
    That is just I was looking for!

  2. [...] Display sticky post and exclude it from recent posts list in WordPress wird beschrieben wie man den sticky-Artikel aus der Auflistung der aktuellen Artikel [...]

  3. Harry says:

    Hi it could be exatly what I was searching but it does not work. I am not very keen in php but I have inserted the code instead of the loop (I am using e default index.php) and nothing happened…

  4. I am figuring out this sticky problem for days… it looks so simple.

    Though I have a lot experience with wordpres.. and still I can not get it to work.

    Please help..

  5. Mark says:

    Thanks man. This really helped me out as I was looking for a way to have sticky posts with my custom page that only shows a certain category.

  6. Thanks for posting this WordPress code. I’m giving this solution a try. Hopefully, I can get it to work!

  7. Nice post but it will be more helpful if you can provide some styling code on how the sticky post can be showed like a featured post. Thanks in advance.

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Code Snippets (15 of 34 articles)