Sticky Posts For Categories

A number of requests have been posted for a way to use sticky posts when displaying a category. The code below can be used to modify your category.php to use for all categories, or as category-id#.php to use for a given category id.

The code is an example only.  It must be used to modify your code to fit your theme.

<?php
/*
Template to show sticky post(s) for categories.
*/
?>
<?php global $more; $more = 0;?>
<?php get_header(); ?>
<div id="content"><!-- file:StickyCategory.php -->
   <?php $categories = get_the_category();
		if ($categories) {
         foreach ($categories as $onecat) {
            $these_cats[] = $onecat->cat_ID;
         }
         $paged = get_query_var('paged');
			$stickies = get_option('sticky_posts');
			$args = array( 'category__in' => $these_cats, 'paged' => $paged,);
			if (!$paged) {  // Only show stickies on first page
				$myquery_hold = $wp_query;
				$args['post__in'] = $stickies;
				query_posts($args);
            $found_posts = 0;  // Use this to not show warning if only stickies
            if (have_posts()) {++$found_posts; bmc_show_category($nav=false);};
            unset($args['post__in']);
            $wp_query = $myquery_hold;
         }
         $args['post__not_in'] = $stickies;
         query_posts($args);
         if (have_posts()) {
            bmc_show_category($nav=true);
         } elseif (!$found_posts) {
            echo "<h4>There are no posts in this category $this_cat<h4></h4>";
         }
      } else {
         echo "<h4>There are no categories for this request</h4>";
      } ?>
</div><!-- content -->
<?php get_footer(); ?>
<?php function bmc_show_category ($nav = true) {
   while (have_posts()) : the_post(); ?>
      <div class="post" id="post-<?php the_ID(); ?>">
      <hr /><h2><a href="<?php echo htmlentities(add_query_arg('stayincat',get_query_var('cat'),get_permalink())) ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

      <div class="entry">
         <?php the_content(' (more...)'); ?>
      </div>
      <?php if ($nav) { ?>
         <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />');
         ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>
         <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
         </p>
      <?php } ?>
      <?php  echo "<a class=\"pageprevlink\" href=\"";
         the_permalink();
         echo "\"> Comment or Reply </a>";
      ?>
      </div>
   <?php endwhile; ?>
   <?php if ($nav) { ?>
      <div class="navigation">
      <div class="alignleft"><?php next_posts_link('&laquo; Older Entries &laquo;') ?></div>
      <div class="alignright"><?php previous_posts_link('&raquo; Newer Entries &raquo;') ?></div>
      </div>
   <?php } // End of if $nav ?>
<?php } // End of Function bmc_show_category ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

*