Did you know that WordPress allows you to feature your posts by using sticky posts feature. However, sticky posts are one of the least known features of WordPress. In this article, we will show you 6 cool things you can do with sticky posts in WordPress.
Video Tutorial
If you don’t like the video or need more instructions, then continue reading.
1. Automatically Expire Sticky Posts
If you are using sticky posts to highlight a special event or coupon, then you will need to unstick the post once that event is over.
This sounds like unnecessary work that you should automate.
Simply install and activate the Expire Sticky Posts plugin. Upon activation, you can set expiry date for sticky posts.
After the expiry date, your sticky post will automatically become a normal post. For detailed instructions take a look at our tutorial (with video) on how to set expiration date for sticky posts in WordPress.
2. Sticky Posts for Categories
By default, sticky posts only appear on the front-page of your site. But what if you wanted to display featured content on your category archive pages?
You can do that by installing and activating the Category Sticky Post plugin. Upon activation, edit a post that you want to feature and select the sticky post category.
For more detailed instructions, see our tutorial on how to add sticky posts for categories in WordPress.
3. Display Latest Sticky Posts
Typically sticky posts are used for featured posts to display your most prominent content. But after a while your old featured posts disappear under the archives. You can bring back your old featured content to life by showing them on custom archives page or anywhere else on your site.
Simply paste this code in your theme’s functions.php file or a site-specific WordPress plugin.
function wpb_latest_sticky() { /* Get all sticky posts */ $sticky = get_option( 'sticky_posts' ); /* Sort the stickies with the newest ones at the top */ rsort( $sticky ); /* Get the 5 newest stickies (change 5 for a different number) */ $sticky = array_slice( $sticky, 0, 5 ); /* Query sticky posts */ $the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) ); // The Loop if ( $the_query->have_posts() ) { $return .= '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); $return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>'; } $return .= '</ul>'; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); return $return; } add_shortcode('latest_stickies', 'wpb_latest_sticky');
After adding this code, simply create add the shortcode [latest_stickies]
wherever you want to display your latest sticky posts.
For detailed instructions, visit our article: How to display latest sticky posts in WordPress.
4. Sticky Posts for Custom Post Types
Sticky post feature is only available for WordPress posts, but this does not mean that you cannot add this feature for other post types.
Simply install and activate the Sticky Custom Post Types plugin. Once you have activated the plugin, visit Settings » Reading and enable sticky posts for any post type you want.
For more detailed instructions check out our tutorial on how to add sticky posts in WordPress custom post types.
5. How to Hide Sticky Posts From WordPress Loop
When using sticky posts, you will notice that by default WordPress displays your sticky post at the top of all your WordPress posts. For example, if you have a loop to show recent posts, then sticky posts will appear on the top no matter when they were added.
To avoid this simply use ignore_sticky_posts
argument in your WordPress query, like this:
<?php $args = array( 'posts_per_page' => 10, 'ignore_sticky_posts' => 1 ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
See our tutorial on how to exclude sticky posts from WordPress loop for more detailed instructions.
6. Styling Sticky Posts
Want to add custom styling to your sticky posts?
Many WordPress themes use post_class()
function to automatically add post classes for each post. If your theme is already using post_class() function, then you will see sticky class added to your sticky posts.
If your theme is not adding sticky class to the post container div, then you can add that yourself by adding post_class() function into the post div or article container.
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
Now you can use the .sticky
CSS class in your child theme‘s stylesheet. Here is some basic CSS to get you started:
.sticky { background-color:#ededed; border:1 px solid #f5f5f5; color:#272727; padding:5px; } .sticky:before { content: "Featured"; color: #FFF; background: #f20000; padding: 10px; display: inline-block; text-align: right; float: right; font-weight: bold; text-transform: uppercase; }
This is how it looked on our demo site using Twenty Twelve theme.
That’s all, we hope this article helped you learn some cool things to do with sticky posts on your WordPress site. You may also want to check out our guide on 10 most wanted category hacks and plugins for WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.