PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Highlight Featured Posts

Posted by on 15th Jun 2008 | 3 comments

Sometimes you want to write a post that stands out from the rest. Usually giving the selected post an additional CSS class will do the trick, but how do you work out which posts need highlighting? This is simply done by creating a new category called ‘Featured’ (note you can call the category anything you like, but I’ll refer to it as Featured in this post).

If you create a new category called ‘Featured’ to highlight your posts, you can choose whether to also keep this category displayed on the category listing. If you choose not to then just exclude it from the wp_list_categories() tag.

So, when you create a new post you assign it to your usual category but you also assign it to the featured category if you want it to be highlighted. Simple enough? Then with one small change in your index.php file you can easily get an additional class added to the post container.

in_category() is the template tag to check if a post is in a particular category. It’s a conditional tag ie. it either returns TRUE or FALSE so is always used in a PHP if statement.

if (in_category('featured')) :
// the post is in the featured category
endif;

So using this conditional tag we can easily target the posts in the index.php template. You want to insert a class into the wrapper of the post. This will most likely be a div. Using the Classic theme as an example we originally have:

<div class="post" id="post-<?php the_ID(); ?>">

So we need to insert a second class into this div if it’s a featured post. To do this we use the following code:

<div class="post<?php if (in_category('featured')) echo ' posthighlight' ?>" id="post-<?php the_ID(); ?>">

Note the additional space before the posthighlight to separate the post class from the posthighlight class.

You can then target the posthightlight class in your CSS (make sure you put this class after your post class settings, so that you override them with the posthightlight settings).


A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

3 comments - Leave a reply
  • Posted by Todd Andrews on 16th Jun 2008

    I recommend using plugins to automate this process for you. I have experienced with a couple WordPress plugins that have taken care of it for me.

  • Posted by Sarah on 16th Jun 2008

    Todd, my posts are on WordPress coding so by way of example it's easier to understand the wide variety of template tags available to users. Also, I would think a plugin would be overkill to do the same as in_category() does!

    Plugins are usually available for virtually every job possible in WordPress, but a plugin requires information in your database (so that WordPress knows it's activated) and is usually a lot longer than one line of code. If you're not scared of a little code then avoiding plugins if/when you can will leave your site faster and most likely less prone to security holes caused by out of date plugins. :)

  • Posted by ru4real? on 23rd Nov 2008

    What a great idea! I'm going to tuck this little nugget away for future use. I'd never considered doing this before now, but I see definite potential. Thanks for teaching us Sarah!