I’d like to be able to show sticky posts at the top of category pages. I’m using archive.php for my category page.
I’m using the code below to display sticky posts at the top of my category archive page followed by the rest of the posts within that category.
This works fine, until there are no sticky posts to display within the category and then it duplicates the posts list.
<?php
// get the current category
$category = get_the_category();
// get the sticky post in the category, order by title - ascending
query_posts(array( 'post__in' => get_option('sticky_posts'), 'orderby' => 'title', 'post_date' => 'DESC' , 'cat' => ''.$category[0]->cat_ID.'' ));
?>
<?php if (have_posts()) : ?>
<?php
if ($cat)
{echo "<h2>Articles in " . get_the_category_by_ID($cat) . "</h2>";}
?>
<ul id="archive-list">
<?php while (have_posts()) : the_post(); ?>
<li class="sticky"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Updated on <?php the_modified_date(); ?> at <?php the_modified_time() ?></span></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php
// get the sticky post in the category, order by title - ascending
query_posts(array( 'post__not_in' => get_option('sticky_posts'), 'orderby' => 'title', 'post_date' => 'DESC' , 'cat' => ''.$category[0]->cat_ID.'' ) );
?>
<?php if (have_posts()) : ?>
<ul id="archive-list">
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Updated on <?php the_modified_date(); ?> at <?php the_modified_time() ?></span></li>
<?php endwhile; ?>
</ul>
<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
<?php else : ?>
<h1 class="center">Sorry, no articles have been published in the <?php if ($cat) {echo "" . get_the_category_by_ID($cat) . "";} ?> category.</h1>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
<?php endif; ?>
Any help would be greatly appreciated!
Thanks.
try to use a conditional statement to wrap the first loop, such as:
add the corresponding
endif;
after theendif;
of the first loop.Use
wp_reset_query()
afterquery_posts()
. We have lots of posts on this. 😉