Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
ant the pagination? what is the way to integrate the paginate_links() here? –  juan Apr 27 '12 at 14:12

2 Answers 2

up vote 2 down vote accepted

try to use a conditional statement to wrap the first loop, such as:

if( get_option('sticky_posts') ) : //only do the next part if sticky posts

add the corresponding endif; after the endif; of the first loop.

share|improve this answer

Use wp_reset_query() after query_posts(). We have lots of posts on this. ;)

share|improve this answer
    
Thanks for your answer, but I'm not exactly sure where wp_reset_query() is supposed to go within my code. I have two instances of query_posts(); which one does it go after, and does it need to go inside its own <?php tags? –  Darren Apr 6 '11 at 10:28
    
It goes after the loop’s end – have_posts() endif; – and yes, since it is a PHP function it has to happen inside PHP tags. :) –  toscho Apr 6 '11 at 10:58
    
Thanks for you help (and patients!) @toscho. I've added <?php wp_reset_query(); ?> after <?php endif; ?> for each loop, but it doesn't have any effect. –  Darren Apr 6 '11 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.