Tutorial : how to create a featured article section

I’ve been craving for a hack or plug-in that could create a featured article on my blog, a sticky kind of article that would show up on every of my pages, above the rest of all posts and stand out a little - all this without too much hassle. Unfortunately, I couldn’t find a plug-in that would do the dirty work for me, so I had to improvise - after a bit of digging I ended up on Maxpower blog, reading an article wrote by deepthought.

After wrestling a little with the code, I found another article wrote by Solostream , but that wasn’t too helpful either - so I knew I had to take the matter into my own hands.

How do I get started ?

For starters, you should download this file index.txt , and rename it to index.php. This will be your new index.php file, upload it to your main Wordpress folder, not before renaming your original index.php to index-bk.php - it’s safe to keep a back-up.

Things will look a bit messy before you’ll get everything working, so I suggest getting the Maintenance Mode plug-in .

This code looks really messy, what does it do ?

Actually, the code for this is pretty simple, and it utilizes the former code you had in your original index.php file.

1.
<?php $my_query = new WP_Query('category_name=featured&showposts=1'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>

2. <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

What does this do ?

Basically, the ‘hack’ we’re going to use, utilizes two loops - one that gets the post from a certain category, in this case featured and shows a single post from that category - the latest.
The second code is actually your second loop, the loop that gets the rest of the posts and displays them accordingly.

The rest of the code should actually be the one you had in your index.php before. By looking at the index.txt file I’ve provided, I hope I’ve made things a bit more clear. It’s self explanatory that the class called featuredpost is used to style the featured post, while the post class is used to style the rest of the posts. Basically, this code should work out of the box without too much hassle as you’re using the former pre-defined CSS classes of your theme and using the original code from the index.php .

If you have any questions, please don’t hesitate to ask them - I’ll help you as much as possible to get it working .

If it's your first time here, you might want to subscribe to my RSS feeds or join my email subscription list ( just enter your email address in the form on the right side and click submit ).

Add to Bloglines Add to Google Add to Netvibes Add to Newsburst Add to Newsgator Add to My Yahoo

Enter your email address: