Featured posts on your WordPress category pages

While working on a new WordPress theme for my day job, I needed to have the ability to add featured posts to the category pages. WordPress makes things easy with adding custom fields to posts. By adding the field “featured” with a value of “true” to for the post you want to feature, makes it easy to target that post when it comes time to edit your category page template.

This first part looks for the custom field with a true value:

    // this loop is specifically for the Featured Article of a specific category
    // it looks for the 'featured' meta value of 'true', and only in the currently displayed category
    if (is_category( )) {
        // this grabs the current category slug
        $cat = get_query_var('cat');
        $yourcat = get_category ($cat);
        // echo 'the slug is '. $yourcat->slug;

    // this is the query to sort the featured meta_key only with the CURRENT CATEGORY by slug
    $args = 'meta_key=featured&meta_value=true&category_name='.$yourcat
    >slug.'&category_title='.single_cat_title( '', false );
    query_posts( $args );

I left a few of my comments in there from where I was just spitting things out on the screen while I was working out the kinks, like the last line in the top if loop. By adding the meta_key argument to the first post query, and the meta_value you art sorting for that post marked as featured.

The second part below, is the loop for the above query that actually spits out the featured article. It only shows 1 entry too, so if you forget to remove the featured value from a previous post, it won’t show them all. Additionally, if there are no posts marked as featured, none of the code from the “featured” block will display, so you can style it any way you want, and not worry about any remnants displaying and messing up the rest of the page if no posts are featured.

// The Loop
if (have_posts()) : $count = 0;
    while (have_posts()) : the_post(); $count <= 1; $count++;
        $featured_post_id = get_the_ID();
        echo '<section id="featured-area">';
        echo '<div class="featured-title">Featured Article</div>'; ?>
        <article id="post-<?php the_ID(); ?>" class="featured-entry">
                <h1 class="entry-title"><a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>" onclick="_gaq.push(['_trackEvent', 'promo', '<?php the_title(); ?>', 'click']);"><?php the_title(); ?></a></h1>
            <div class="entry-content">
         <?php the_content('Read more'); ?>
      </div><!-- .entry-content -->
    </article><!-- #post-## -->
 <?php endwhile; ?>
 <?php endif; ?>                        
<?php wp_reset_query(); ?>

The last thing I did was reset the query in preparation for the Loop to run to display the remaining posts in this category.

The only other bit I added for the remaining articles Loop is this:

// this is the query to sort the featured meta_key only with with the CURRENT CATEGORY by slug
$args = 'category_name='.$yourcat->slug.'&category_title='.single_cat_title( '', false );
query_posts( $args );

This takes the featured post out of the list of all of the other posts in the category, so it’s only listed once.

%d bloggers like this: