How to Make a Featured Post Carousel for WordPress

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 233×130px 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 233×130px.

See It in Action

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

Related Posts

Add Comment

Discussion 78 Comments

Comment Page 3 of 3 1 2 3
  1. Nice tutorial, thanks! After much hammering to get it fit into our template, I managed to get it to work in our prototype site, but I still need some way to integrate that with qTranslate.

    Now, i have a question: on your example, one can see some text on a semi-transparent layer. Is this supposed to be part of the image or does it get added automagically by the code? I’m guessing the latter, but I’d appreciate confirmation.

    It would be very cool to have this as a plug-in, with 1) image auto-resize, 2) text layer.

    Many thanks in advance!

  2. Morgan Daly says:

    Hello,

    I would like to be able to do a similar thing on a project soon but was wondering if it was possible to do by pulling in the title of the post automatically and maybe pulling in the already uploaded thumbnail without using custom fields?

    Thanks

  3. Cool Post ! always thought to make a post featured :D

  4. nicetravel says:

    very nice tutorial .
    thank you …

  5. CgBaran Tuts says:

    Very useful tutorial thanks

  6. Sajid says:

    Awesome! Some new stuff that the other wp blogs haven’t listed. Thanks!.
    Thanks for the tips.

  7. Sajid Latif says:

    I think I got some result. Thank you here.
    That theme is useful for me and hope for others also.

  8. Seth Rubenstein says:

    Anybody know how to have the posts excerpt in the element?

    Thanks,

  9. Latif says:

    very nice tutorial .

  10. Awesome! Some new stuff that the other wp blogs haven’t listed. Thanks!.

    Now find Online Wordpress Theme at my Website…..check it.

  11. Namuel says:

    the load of the demo URL let you fly into a infinite loop … It doesn’t load anything … at least in my browser.

  12. baloot says:

    how to apply on thesis theme?
    anyway, thanks for this tuts.

  13. Ben Herson says:

    Great tutorial! Is it possible to build this as a horizontal carousel for a sidebar widget?

    Has anyone had any luck with that?

    Thanks!
    Ben

  14. Ben Herson says:

    Sorry – I meant vertical!!

  15. Suneel says:

    I am confused over how to setup a permanent size for the carousel so that it gets displayed on single posts without obstructing the view space.

    Thanks for sharing this. I am trying hard to get it done.

  16. Agung says:

    i get some trouble here,..

    in the step 5 , what plugin that u use it ?

  17. sami says:

    Great tutorial as always, thanks .
    I didnt actually attempt to do it (YET) as I was just browsing and when I tried to view the demo i got an ERROR MSG. I guess thats not how its supposed to be so wanted to let you guys know about it.
    The error is “this page isnt re-directing properly! Firefox has detected that the server is redirecting the request for this address in a way that will never complete.”
    I tried it a few times so I am guessing it isnt my browser..

  18. bobby says:

    I would love to check out the demo you provide but the demo page throws an error. It “redirects in a way which will never complete”, says Firefox. Could you look into that?

  19. Andrei says:

    hey guys the link for the demo at the top of the page is broken,
    just thought you should know

  20. Sean P. says:

    Anyone know how to add the excerpts for the featured posts?

  21. Xesbeth says:

    nice, thanks.

  22. ray says:

    that is what i look for thanks

  23. the demo link is broekn

  24. ariauakbar says:

    How to show the post title of each post on it ?

  25. Vyacheslav says:

    Nice tutorial. It isnt hard to make a “real” carousel on this base using jQuery.
    Thank you.

  26. thnak u so much for such a nice and descriptive tutorial…

  27. Yasin says:

    I seem to be getting a DATABASE errror on your demo file. Could you please advise where else I can see a demo. Thanks, sorry for the trouble.

Comment Page 2 of 2 1 2

Add a Comment