When you set a single post to be “sticky”, the default WordPress behaviour is to “stick” to it to become the very first post of the loop in the Home page. However, there are times where you want rearrange the sticky post and for it to appear only on the third, fifth, or any order you want, post position. There isn’t any plugin that is able to do this as this is more of a theme-related feature. Here is a small hack that I have written to change the sticky post order.
In the following example, I am assuming that there is only one sticky post in action, and it is switched to the third position.
Also, I only want the changes to be on the Home page, so I will only work on the
home.php file in your theme folder. If your theme doesn’t come with a
home.php file, duplicate a copy of “index.php” and rename it as “home.php”.
1. Open “home.php” file in your theme folder.
2. At the very beginning, just before the
get_header(); code, add the following code:
What the above code does is as follow:
- Check the first post in
$wp_queryand see if it is a sticky post.
- If yes, create a new array and set the second and third posts of
$wp_queryto be the first and second elements of the array.
- Add the sticky post as the third element of the array.
- Remove the first 3 posts from the old list of posts.
- Combine the 2 arrays together and assign this new array to
Don’t forget to upload and replace the “home.php” file in the server with this modified code.