Everything Photoshop Subscribe

How to Make a Featured Post Carousel for WordPress

In Working with CMS's by James Lao

It's becoming more and more common for blogs to feature certain posts at the top of the page. In this tutorial, we're going to show you how to implement this in WordPress. We'll be using the default theme, Kubrik, as our base theme, but it should be adaptable to most themes with some modification. There's very little code and featuring posts is simple.

What we're shooting for

We're going to be modifying the Kubrik theme that comes prepackaged in WordPress to be able to feature posts at the top of the page. This tutorial has only been tested on WordPress 2.5.x but it should work on the 2.3.x series as well. We're going to assume you're using 2.5.x or above. By the end of the tutorial, you'll have something like this:

 

Step 1 - Creating the default image

Before we do anything, go to the themes folder of your WordPress installation (wp-content/themes/) and make a backup of the "default" folder. This is the Kubrik theme we will be editing. The backup is in case you want to revert back to the original, unmodified theme.

First, we're going to make a default image in the event no featured post image is specified. Let's keep it sweet and simple for this tutorial. Open up your preferred image editor and create a 233x130px rectangle with 10px radius rounded corners. I made the background a white to grey radial gradient and put some text on top. This is what I have:

Save the image as "no-featured-image.jpg" in the "images" folder that is inside the "default" folder.

Step 2 - Add the PHP code

Now for the code. Open the "header.php" file inside the "default" folder. At the end of the file, you will see a div block and an hr tag like this:

<div id="header">
	<div id="headerimg">
		<h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1>
		<div class="description"><?php bloginfo('description'); ?></div>
	</div>
</div>
<hr />

Between the ending div tag and the hr, insert the following code:

<div id="featured">
	<ul id="carousel">
		<?php
		$featured_posts = get_posts('numberposts=3&category=1');
		
		foreach( $featured_posts as $post ) {
			$custom_image = get_post_custom_values('featured_image', $post->ID);
			$image = $custom_image[0] ? $custom_image[0] : get_bloginfo("template_directory")."/images/no-featured-image.jpg";
			printf('<li><a href="%s" title="%s"><img src="%s" alt="%s" /></a></li>', get_permalink($post->ID), $post->post_title, $image, $post->post_title);
		}
		?>
	</ul>
	<div class="clear"></div>
</div>

This code will output three images in an unordered list. Each image is a link to a featured post. We'll talk about how to configure the code after we add the CSS.

Step 3 - Style with CSS

Next we need to add some CSS styles. Open up the "style.css" file and put the following code below at the end of the file. All this does is float the list elements to the left and space them out evenly.

/* Featured Post Carousel */

#featured {
	padding: 10px 10px 0 20px;
	}

#carousel {
	list-style: none;
	margin: 0;
	padding: 0;
	}

#carousel li {
	float: left;
	padding: 0;
	margin-right: 10px;
	}

Step 4 - Understanding the code

Let's take a look at what the code we added does. Inside the container div (id="featured") we have an unordered list and some PHP code to generate list elements.

$featured_posts = get_posts('numberposts=3&category=<strong>1</strong>');

The first line shown above retrieves the post information using the get_posts() function and assigns the post data to the $featured_posts variable. The get_posts() function excepts a single parameter in the form of a query string similar to what you might see at the end of a URL (sans the initial question mark). The first parameter is "numberposts" which we've set to 3 for this tutorial. This parameter sets how many featured posts we will be showing. The second parameter is "category" which we've set to 1. The value of the "category" parameter should be the ID of the category you are using for your featured posts. You can find the ID of a category by going to the category management page and hovering your mouse over a category title. The status bar will show a link. The last number is the category ID.

The next line is a foreach loop that will loop through the posts we've retrieved using the get_posts() function. The first line inside the foreach loop retrieves the URL of the image using the get_post_custom_values() function and stores the URL in the $custom_image variable. The first parameter specifies the key of the custom value we're using, "featured_image". The second parameter specifies what post we're getting the value from.

$custom_image = get_post_custom_values('featured_image', $post->ID);

In the next line we do a quick check to see if an image was indeed specified. If no image was specified, we assign the $image variable the URL of the default image. If an image was specified, we use that.

$image = $custom_image[0] ? $custom_image[0] : get_bloginfo("template_directory")."/images/no-featured-image.jpg";

In the last line we actually output the list elements. Each element is an image that links to the featured post.

printf('<li><a href="%s" title="%s"><img src="%s" alt="%s" /></a></li>', get_permalink($post->ID), $post->post_title, $image, $post->post_title);

Step 5 - Creating Featured Posts

That's it! Now, whenever you want to feature a post, assign it to the featured category and create a custom value with a key of "featured_image" and a value of the image URL. Images should be 233x130px.

See It in Action

You can view the theme in action on our NETTUTS WordPress Demo server:

Enjoyed this post? Your vote is always appreciated!! Delicious StumbleUpon Float Digg

Comments

Leave a Comment
  1. Pretty interesting stuff, thanks.

  2. Cool. Thanks.

  3. Sweet!

  4. Is this turning into wordpresstuts? I’m not finding very applicable stuff here anymore because I don’t have wordpress.

  5. Is there any way you know of to extract an image from a post without having to create a custom field? WP spits out all images wrapped in paragraph tags, making it difficult to style separately from the rest of the paragraphs.

  6. nice, thanks.

  7. @Braden: Hiho! Yes it has been a little WordPress centric lately! But don’t worry I’ve just loaded 3 non-WP posts into the system for the next three days :-)

  8. I see no problem regarding wordpress, as long as there is a demand for it. Lots of people are using it and will do so in the future. I do however expect the non-wp tuts to come ;)

  9. That was very nice.. thank you for sharing this one.

  10. Why is the word carousel in the title? I don´t think it is a carousel. When I read it, I thought you would be able to click and navigate forward and backward, through more posts.

  11. Interesting, thanks :) Glad to see a few non WP posts are coming though!

  12. Yay for wordpress :)

    thanks for posting.

  13. Is putting the image URL in a special field the best solution? I think it would be better to fetch the image from the post uploads.

  14. Love your Wordpress tutorials, keep up the good job!

  15. Very nice - I see how I could have optimized my own tutorial on this same topic a little better… http://www.cagintranet.com/archive/wordpress-tip-3-awesome-custom-field-tricks/

  16. Awesome stuff. Amazing that you can do something so cool with so little code. Thanks!

  17. Nice tut, but wheres the “carousel”? All I see is static featured images.

  18. @Collis
    THANKS! Can’t wait to see them!

  19. Your articles and advices are always very well done … If I had time, it is with pleasure that I will have put them in action ! Thanks a lot ! (a french lector)

  20. Gravatar

    Nouman Saleem

    wait….who’s not using wordpress?

    haha. I don’t mind em, but the in general tutorials are great as well.

  21. I’m hoping for less Wordpress content as well. Sounds like it’s coming though, so I’m excited. I like what non-wordpress stuff is here so far.

  22. This is very helpful to us WP developer. Thanks!

  23. Please keep the Wordpress tuts coming. They’re incredibly helpful, relevant, and desired!
    Thanks!

  24. I’ve been looking to do something like this with one of my blogs. Thanks for the tut, that should give me the motivation I need to actually get it done!

  25. Nice, though I was expecting something that maybe was animated…

  26. Gravatar

    Christopher Rogers

    Awesome tutorial, exactly what I was looking for. But I can’t seem to get the article title to appear on the bottom of the image as you do in the demo. The images are loading fine. Can you tell me what to look for?

  27. awesome stuff! Will have to get this going when I get home! Thanks

  28. Great Tutorial, but like another said, where is the carousel?

    For those looking for a carousel, check out my site:
    http://wpforsale.com/premium-wordpress-plugins/wordpress-image-carousel-plugin/

    I am using it on my home page.

  29. Christopher Rogers - In the demo the section titles are actually over the background image in one jpg. So you have an image preview for the last 3 entries from a specific category, in this case category 1.

    I guess they decided to call this a carousel in that it will auto update or push the newest post with image up into one of those 3 spots.

    Instead of having 3 images up top for your last 3 posts and have summaries of the same posts underneath you can also edit your wordpress loop to offset the summaries in the body by 3(or any number you choose) so that you see your latest 3 images up to and the 4th with summary in the body of the site.

    Giuseppe - You can edit the php to look for your custom field image in the upload folder within wordpress. Doing so you would be able to use the post upload feature built into wordpress and all you would need to put into the value field would be year/month/filename IE 2008/07/file.ext

    Only drawback to uploading thumbs, or main images via the upload file(s) in WP2.6 is that all uploads via the uploads file(s) button link the uploads to the post. So if you were upload a gallery with 5 images and a thumbnail to a post, and inserted that gallery into your post you would see 6 images since your thumbnail is included. Easy work around…create a page solely for your thumbnails so that they are not included with any other galleries :)

    Hope this helps!

  30. it’s cool, thanks a lot!

  31. Awesome post, this is just what I was looking for, in order to complete my new blog design. But, dude please tell me what editor you are using in the images above. It looks awesome. Let me know when you get a chance.

    Thanks again!

    Bryan

  32. I just had to do something like this for a client, and ended up contriving some convoluted solution having to do with categorization and tagging, outrageous PHP and so forth. This is FAR more elegant; I think I’m going to move to this model.

    Thanks!

  33. Hey - I wrote a day or so ago, and never heard back. Just a few questions if you wouldn’t mind answering them?

    1. Is there any way to, instead of using a default image when none is specified, use no image at all (so it’s just normal text with no image)?

    2. What editor are you using in the images above?

    Thanks,

    Bryan

  34. Hey, I would love to know if there was a trick out there that would do the thumbnails automatically, because I know myself. If I would add this, I’d do it for a month or so and then get too lazy to do thumbnails and thus not feature any posts anymore. :(

    Somebody an idea or helping link? I googled, I failed.

  35. @Bryan
    1. Yes. Instead of using the ternary operator to assign the $image variable, just test $custom_image[0] with an if statement and show an image on true and whatever text you want on false.

    2. Not sure what you mean by ‘editor’. If you mean the admin style, it’s new in WordPress 2.6.x

    @Phil
    Look into the GD library for graphics processing.

  36. thanks, i discover yesterday nettuts.com, very useful things for me.

  37. Very nice, has anyone done this with pages in stead of posts?

  38. like it

  39. That’s pretty neat :) I am bookmarking this to play with later on my next redesign kick :)

Add a Comment

Note: We use Gravatars on NETTUTS, they are little icons that appear next to your name on this site and on many others. You can get a Gravatar account for free and any other site that supports it will show your avatar too!

 

Trackbacks

Leave a Trackback