How to Make a Sticky Post for WordPress Blog


A Featured post or sticky post is being used in most of the themes. It is a post that will always be on top of all your recent posts and stay there until you change it. You can use this sticky post to highlight your best post. It will also serve as a notification, reminder or something that you want to emphasize to your readers. Here is how you can make a sticky post..

 

First of all, Select a category name that will hold all your sticky posts. For example, you can create a new category and name it “Sticky” and later use this name to call the posts from this category. Now, if you have ever opened the index.php file in any blog styled WordPress theme, you will notice that it has a loop which is used to call your posts.

 

The loop begins here

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

 

and this loop should end here

<?php endwhile; else: ?>
<p>Sorry, nothing matches that criteria.</p>
<?php endif; ?>

 

Copy everything from the start to the end of this loop (including the above code). Paste it above the existing loop so that index.php will have two identical loops, one over the another. To make the sticky post appear, we will use this new loop which will call a single post from the “Sticky” category.

In the top loop, just replace this code 

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

with this

<?php if (have_posts()) : ?>
<?php $my_query1 = new WP_Query('category_name=sticky&showposts=1'); ?>
<?php while ($my_query1->have_posts()) : $my_query1->the_post(); ?>

 

So, if you decide to change the category name from “Sticky” to something else, just change it in the above code. You can also change the number of posts that you want to show from the sticky category by modifying the show posts variable. That’s it and you have a sticky post on your blog.

Leave a Reply