You may occasionally come across something called Sticky Posts when reading about WordPress. They’re a relatively unattended feature of WordPress and allow you to highlight selected posts by sticking them on top of your latest posts loop. This comes handy to feature particular posts.
With some efforts it’s possible to style or handle sticky posts differently to really mix things up when it comes to displaying content on your WordPress website. Sticky posts are default functionality in WordPress and should work with any WordPress theme. The popular MH Magazine WordPress Theme supports sticky posts as well, without special styling. Now, let’s have a look at sticky posts and how to handle them correctly on your WordPress website:
- How can you make posts sticky?
- What can you do with sticky posts?
- How can you style sticky posts?
- How can you detect a sticky post while coding?
- Useful WordPress plugins for sticky posts
How can you make posts sticky?
When creating a new post in WordPress, you have the option of making it a sticky post via a checkbox within the Publish panel on the right of the post editor. Simply click the Edit link that is alongside the Visibility label where you will be presented with a few options. You can make the post sticky, protect the post with a password, or make it a private post. Check the Stick this post to the front page checkbox and click Update / Publish the post to make your post sticky.
However, the wording Stick this post to the front page is a bit misleading. WordPress is referring to the Posts page with your latest posts that is displaying by default when you activate a new WordPress theme. In case your front page for example is a static front page based on widgets, the sticky posts won’t necessarily display on top as expected. It’s also important to know that sticky posts on category or tag archives are not support in WordPress out of the box (see plugins).
What can you do with sticky posts?
Posts that you’ve made sticky are displayed at the top of your post loop (usually your Blog / Posts page). So by default, if you would like to see an older post be displayed in a more prominent location, you could make it a sticky post and it will appear above your latest posts on the Posts page. You can also unstick your posts when you want them to return to their natural location within the loop. To do that, simply uncheck the sticky checkbox when editing that particular post.
How can you style sticky posts?
If the particular WordPress theme you’re using makes use of the post_class() WordPress core function, then each of your sticky posts will have been assigned a CSS class that you can use to style your sticky posts independently from your standard posts.
For example, using the default Twenty Sixteen theme, each post within the loop is wrapped within an
1 | <article> |
tag that contains the post ID and numerous CSS classes. These CSS classes allow you to identify a variety of information regarding each particular post, like so:
<article id="post-123" class="post-123 post type-post status-publish format-standard hentry category-uncategorized">
These CSS classes tell us that it is a published post using the standard Post Format and is within the Uncategorized category. The hentry class is for microformats / structured data, you can ignore this class for now. To easily determine these classes, you can use tools like Firebug.
There are additional post classes that you may also come across. For example post tags, password requirements, if the post has a featured image or not, its Post Type, along with all taxonomies related to the post, including sub-categories for example. For a complete rundown of the classes available, visit the post_class reference page. By making this particular post a sticky post, it will also add the sticky class to this array of post classes, like so:
<article id="post-123" class="post-123 post type-post sticky status-publish format-standard hentry category-uncategorized">
This allows you to use the .sticky class in your CSS code to style your sticky posts different from your regular posts in case your WordPress theme doesn’t already include special styling for sticky posts.
How can you detect a sticky post while coding?
If you’re a developer and want to do funky stuff with sticky posts, that is possible as well. You can use the is_sticky() WordPress core function to detect if one of your posts is a sticky post, like so:
if (is_sticky()) { // Sticky post content }
Or use an if…else statement, like so:
if (is_sticky()) { // Sticky post content } else { // Normal post content }
This allows you to manipulate your sticky posts in any way you require, a simple example can be found within the template-parts/content.php file of the WordPress default theme, Twenty Sixteen:
<?php if (is_sticky() && is_home() && !is_paged()) : ?> <span class="sticky-post"> <?php _e('Featured', 'twentysixteen'); ?> </span> <?php endif; ?>
This is a conditional statement to check if a post is sticky or not (as well as check if you’re viewing the homepage and not viewing a paginated page / archive). As soon as those three conditions are met, the translatable string Featured will be displayed above each of your sticky posts. So not only is the sticky post in a very prominent location, at the top of the post loop, but the reader is also informed that the post is a featured post and not just a regular post.
Useful WordPress plugins for sticky posts
There are a couple of useful plugins available in the WordPress Plugin Directory that will further enhance the functionality of the sticky post feature. We picked two great WordPress plugins for sticky posts that we think add some useful functionality, but for sure there are more available:
Category Sticky Post
As mentioned earlier in this article, WordPress doesn’t support sticky posts on category archives out of the box. The Category Sticky Post WordPress plugin allows you to stickify posts at the top of a specific category. This plugin comes with an unobstrusive, lightweight design and should integrate nicely with any WordPress theme you are using. It is fully localized and translation ready.
Lonely Sticky
The Lonely Sticky plugin enables you to control how many posts can be stickified at the same time. This is especially useful if you don’t want your blog or posts page to be filled with just sticky posts, as each subsequent post you stickify will unstick other posts. That way you can keep your sticky posts under control without any effort on your behalf.
Conclusion: Sticky Posts in WordPress
If you would like to make use of the sticky posts feature, then check in what ways sticky posts are already integrated into your WordPress theme. Is there a special styling? How and where does your WordPress theme display sticky posts, etc. …? To individually customize sticky posts for your website, you can apply styling with some CSS yourself. And, you could search for some suitable plugins that add additional functionality to your sticky posts if needed.