This is the source of all your problems:
<?php if(is_home() && !is_paged()) { ?>
<?php query_posts(array('post__in'=>get_option('sticky_posts'))); ?>
This takes the current query, and puts it to one side, and places a new query in its place. Here you’re saying that you want to show the first page of sticky posts. You said nothing about tags or categories, etc, remember, computers do exactly what you tell them, and you said nothing about the context you’re in.
So what you want is the first 5 posts to be marked differently from the rest. So instead of:
while ( have_posts() ) {
the_post();
// it's a post! display things!
}
Do:
$counter = 0;
while ( have_posts() ) {
the_post();
$counter += 1;
if ( $counter <= 5 ) {
// it's one of the first 5 posts! display the special top 5 post!
} else {
// it's a post! display things!
}
}
Alternatively, when you reach the 6th post, exit the loop and start a new one:
$counter = 0;
while ( have_posts() ) {
$counter += 1;
if ( $counter > 5 ) {
break; // we've already shown 5 posts! Exit this loop and start the next one!
}
the_post();
// it's one of the first 5 posts! display the special top 5 post!
}
while( have_posts() ) {
the_post();
// it's a post! Display the post!
}
Going back to your post__in
, if you want to only show sticky posts, you should use a pre_get_posts
filter to modify the arguments passed in to the current query to include your post__in
argument. There’s a number of very good answers elsewhere on the site on how to use pre_get_posts
so I won’t duplicate them here.
As a side note, never use query_posts
under any circumstances. I know a number of employers who would consider a sighting of query_posts
in commercial code a serious event, and a lot of review processes will reject code containing it outright for very good reasons. Use the pre_get_posts
filter if you need to modify what is shown on a page, and if you need a separate query to grab posts, e.g. for a sidebar widget etc, use a WP_Query
object. query_posts
is kept inside WordPress mainly for legacy reasons. Sadly a lot of tutorials out there mistakenly recommend its use