Stumble Digg Technorati Delicious

WordPress Featured Post Slider Using jCarousel

Buzz This!
June 21st, 2009 by Jesse

Recently I wrote about the importance of having a featured a posts section and what it can do for your SEO tasks, to fill this need I used a plugin on this site called featured content gallery and while it was effective it was bloated, slow and conflicted with many of the other things I wanted to try on my site so I started off on a journey to find the most efficient, fast and clean way to showcase my featured content. I searched all over the internet and came up with nothing that did what I wanted how I wanted to do it so I decided to take matters into my own hand and make my own solution to accomplish it.

Now that I have it working on this site I will share how to do it so anyone who is looking for the same thing I was can find it and benefit from it. I was inspired in part by the way johnchow.com displays his featured posts so I started to look at how he was doing it on his site, which lead me to find jCarousel and the rest is history. Simply follow the steps outlined below and you can use this solution on your WordPress powered site just like I do here at AdminDaily.com

  1. Download jCarousel HERE
  2. Extract the zip file and locate the javascript filed named jquery.jcarousel.pack.js and upload it to your server root directory in a folder named js
  3. Create new files and edit your sites theme files as outlined below and as always BACKUP YOUR FILES FIRST

First off, we will make a file named featured.php and place it into your wp-content/themes/your-theme/includes folder, this file will contain the following code, I will explain the functions and parts individually.

At the top of the file, we need to build a container for the featured posts slider, so you have an opening div and the start of an unordered list like so;

<div id="featured">
<ul id="posts">

below this, we will use a custom WordPress query to gather the posts we want for this section, start a counter to keep track of what post number each segment is and then start the loop that will display our posts. In this example, I will be taking the last 5 posts from the Featured category; you can change it to your liking.

<?php $my_query = new WP_Query('category_name=Featured&showposts=5'); ?>
<?php $count = 0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $count++; ?>

Continuing on below this, we will include the code that will be used by the loop to display each post, this is again just an example and you can obviously change it around to suit your particular needs. This is what I use for my site and it does what I need it to.

As you can see, the above code will query the last 5 posts from the Featured category then display them each by looping using a while statement that is concluded at the end with an endwhile statement, if you want to see what each piece generates look at the page source of my homepage.

<li id="post-<?php echo $count ?>" class="first">
<a href="<?php the_permalink() ?>"><img class="thumb" src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" alt="<?php the_title(); ?>" /></a>
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<div class="content">
<p><?php echo get_post_meta($post->ID, 'feat_txt',true) ?></p>
</div>
<div class="oops"></div>
<a href="<?php the_permalink() ?>" class="more-link">read more &raquo;</a>
</li>
<?php endwhile; ?>
</ul>

Finally, we add the last unordered list to the bottom that will display the numbered buttons to click to show the posts in the slider that are numbered 1 through 5 like so;

<ul class="list">
<li><a href="#post-5">5</a></li>
<li><a href="#post-4">4</a></li>
<li><a href="#post-3">3</a></li>
<li><a href="#post-2">2</a></li>
<li><a href="#post-1">1</a></li>
</ul>
</div>

Click Here to download a complete featured.php file to save you some copy/pasting :-)

Simple enough so far right? Now we move on to the edits we need to put in our themes files, first off we will start out with the header.php file. In this file we need to add the jCarousel script that we previously uploaded to the js folder in the site root directory and add the specific function that controls the operation of the slider on the page. We will also limit this to only the home page to save loading times on pages that don’t use it by simply not including it on those pages by using a if (is_home()) statement at the beginning. To accomplish this, simply place the below code into your header.php file before the closing </head> tag. (obviously change the script src to reflect your domain)

<?php if ( is_home() ) { ?>
<script src="http://your.com/js/jquery.jcarousel.pack.js" type="text/javascript"></script>
<script type="text/javascript">
function mycarousel_initCallback(carousel) {
jQuery('#featured .list li a').bind('click', function() {
carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
return false;
});
};
jQuery(document).ready(function() {
jQuery("#posts").jcarousel({
wrap: "both",
auto: 10,
scroll: 1,
initCallback: mycarousel_initCallback,
buttonNextHTML: null,
buttonPrevHTML: null
});
});
</script>
<?php } ?>

Next, we include the featured.php file wherever you wish to show it within your template files using a php include statement like so:

<?php include (TEMPLATEPATH . '/includes/featured.php'); ?>

You will likely want the featured.php file to be included on your index.php theme file, but you are more than welcome to adjust and style to your taste.

Now that we have the files uploaded, the needed edits made and things ready to roll, we just need to add the styling elements to our themes css file, this is the part that needs the most attention. The code I am putting here is what I have used on my site to get it to display how I want, you will need to edit it to suit your needs unless you just happen to have the same width available and want to use the same colors I have on my site. Another note on this, you will want to check this over for any conflicting entries to make sure none of this will mess up the existing layout of your site (just change the names in the featured.php and css file if you need to)

#featured {
width:100%;
height: 199px;
margin:0 0 25px;
font:1.25em/1.5 georgia,serif;
overflow: hidden;
}
#featured:after {
content:".";
display:block;
height:0;
font:0/0 serif;
clear:both;
visibility:hidden;
}
#featured h3 {
font:1.47em/1.1 georgia,serif;
margin:0 0 10px;
color:#1e1e1e;
}
#featured h3 a {
color:#4a8393;
}
#featured h3 a:visited {
color:#4a8393;
}
#featured h3 a:hover,
#featured h3 a:focus {
text-decoration:underline;
}
#posts {
width:636px;
clear:both;
overflow:hidden;
}
#posts li {
float:left;
width:636px;
width/**/:/**/626px;
padding:0 10px 0 0;
}
#posts li.first {
margin:0;
}
#featured .thumb {
float:left;
border:1px solid #d7d7d7;
margin:5px 10px 11px 0;
}
#featured p {
padding:0 0 10px;
}
#featured .more-link {
float:right;
font-size:1.2em;
padding:0 10px;
margin:0 -10px -10px 0;
position:relative;
color:#fff;
background:#404040;
border:1px solid #333333;
}
#featured .list {
float:left;
display:inline;
position:relative;
font-size:.88em;
line-height:2.1;
font-family:arial,sans-serif;
z-index:5;
color:#333;
width:527px;
padding: 0px;
margin:-30px 0 0 0px;
background:#ffffff;
border-bottom: 1px solid #eeeeee;
}
#featured .list li {
float:right;
}
#featured .list li a {
float:left;
color:#333;
background:#f6f6f6;
border:1px solid #eaeaea;
border-right:none;
padding:0 8px;
}
#featured .list li a:visited {
color:#444;
}
#featured .list li a:hover,
#featured .list li a:focus {
color:#fff;
border:1px solid #333333;
border-right:none;
background:#404040;
text-decoration:none;
}
#featured .jcarousel-container {
width:646px;
width/**/:/**/636px;
padding:6px 0 0 10px;
margin:0px;
background:#ffffff;
position:relative;
float:left;
display:inline;
}
#featured .jcarousel-clip {
z-index:2;
position:relative;
overflow:hidden;
width:636px;
}
#featured .jcarousel-list {
z-index:1;
overflow:hidden;
position:relative;
top:0;
}
.oops {
clear:both;
font-size:0;
line-height:0;
width:100%;
overflow:hidden;
display:block;
}

Now that we have all that done, we are almost ready to save all this and start displaying a super cool featured posts slider using jCarousel. All that is left to do is to create or edit posts and add them to the Featured category and configure a few simple things in each post to provided the needed information for the featured posts slider, just complete these steps for each post you want to show in the slider and you will be good to go.

  1. Write new or add existing posts to the Featured category (create the category if you don’t have it already)
  2. Add a custom field named feat_txt by clicking where is says “add new” then enter the text you wish to display as the featured text in the slider (i recommend about 20-30 words for this)
  3. Add another custom field for the image you wish to use in the features posts slider and name this custom field thumbnail and place the complete url to the image you wish you use, in the featured.php file I supplied in this tutorial the image will be scaled to 135px by 135px so using and image of any other size will be a waste.

Here is a screen shot showing how the custom fileds section should look for any post you wish to feature.

screenhunter_01-jun-21-1542

and

screenhunter_02-jun-21-1550

This should be it, if you have any questions please don’t hesitate to post them here. I will be more than happy to help any way that I can.


  • Bob Gillespie
    Hi Jesse, just a heads up, the Featured Content Gallery you mention should be avoided.
    Yesterday I had a site taken down by the js downloader.agent virus. All attempts to upgrade or reload the Gallery files failed. Only complete removal of the Gallery plugin solved the issue.

    Looks like the perfect time to try your script :)
  • Hi Bob,

    Thanks for the heads up on that... I guess now would be a good time to try it out ;-) I'm glad I'm not using that gallery on any of my sites anymore.

    I also have another related post that you might want to take a peek at, about enabling and integrating the new post thumbnails in WordPress 2.9 which you can find at http://admindaily.com/upgrade-your-theme-to-sup...
  • Hi Jesse. Thanks for the quick reply.
    Actually found the solution last night. Check this out:

    <?php $count++; ?>
    <?php global $post;
    $post_image = get_post_meta($post->ID, 'thesis_post_image',$single=true);?>

    <li id="post-<?php echo $count ?>" class="first">
    <a href="<?php the_permalink() ?>"><img class="thumb" src="<?php echo $post_image ?>" width="135" height="135" alt="<?php the_title(); ?>" />

    <a href="<?php the_permalink() ?>"><?php the_title(); ?>


    <div class="content">


    As you can see I insert after the count++

    Sorry, doesn't look like there's a way to highlight in the Disqus interface.
    Keep in mind though this is for thesis :)
  • Ah, yes.. thesis is a different animal :) Thanks for sharing this so others
    that run into it in the future will benefit.

    thanks :)
  • Hi Jesse,
    Love the post, super useful! I got everything to work, but I'm having issues with the post->id query. If i replace it with a post-number, it works fine for that specific post, but when I try to use the post->id variable the image from the post do not show up.
  • Hi, thanks for stopping by and I am glad you found this post useful :)

    I have a related topic you may want to check out, has to do with the new post thumbnails feature that WordPress 2.9+ supports, you can read it here:
    http://admindaily.com/upgrade-your-theme-to-sup...

    Including the post thumbnails this way is much cleaner and more convenient than how I did it at the time of this original post... I think it may be time for a revision or update :-) thanks for reminding me.
  • Hi Jesse, Thanks for the replying so fast.

    Check this out: Remember though i'm using thesis. I insert after count++
    The reason I think your post is awesome is because it lets me create different sliders on different pages which is what I really want to do :)
    <?php $count++; ?>
    <?php global $post;
    $post_image = get_post_meta($post->ID, 'thesis_post_image',$single=true);?>

    <li id="post-<?php echo $count ?>" class="first">
    <a href="<?php the_permalink() ?>"><img class="thumb" src="<?php echo $post_image ?>" width="135" height="135" alt="<?php the_title(); ?>" />

    <a href="<?php the_permalink() ?>"><?php the_title(); ?>


    <div class="content">
  • Yes, having the ability to use it differently on different pages can come in
    handy :)
  • http://www.gradientblend.com/
    I have set this up so that everything is working. The Posts are all set to be Featured and they show up as planned, but for the life of me I cannot get this to show up right as far as the carousel is concerned. Everything seems to be working EXCEPT there is no slideshow or carousel...the posts just display vertically down the page. It's as if the Jquery isn't being fired, but I have checked and the link is pointing to the js file properly. All I need to do is to have the content display in the carousel and I'm done. Can anyone help me out, here? (Beating head against the desk!!!)
    .-= graefika´s last blog ..The Omega. This is the last. =-.
  • I just looked at your page source, I dont see the script portion that actually controls the slider.. you need to look up at the instructions on this page, find the part that starts with the if ( is_home() ) { ?>

    Add that section before the closing head tag and you should be good to go.
  • I pasted the code controlling the slider right above the tag. The code is there (locally that is) - but when I upload it to my server and view source on the same page, the code controlling the slider disappears. I don't know if my server is not allowing me to actually replace the file or what. It's like the code is being stripped out and I don't know why. It could be working properly and I would never know it.
    .-= graefika´s last blog ..The Omega. This is the last. =-.
  • Nash
    Can you write a tutorial on how you created a collapsible blog intro?It's really cool...
  • what do you mean by "collapsible blog intro"?
  • Nash
    Your post starts with an intro element..."If you haven't already, please subscribe to my RSS feed or get updates by e-mail."

    What I'm fascinated by is the [x] in the upper right corner. I know how to make an intro box et al...but I can't figure the [x]. Someone suggested jquery but I'm still clueless...
  • ahh, thats a plugin called wp greet box. You can find it by clicking HERE

    It's very customizable and does quite a few handy things.
  • Nash
    I feel cheated.... :-(

    Gret site on the whole though...I think I actually love some of the simplicity in the design. Underscores rather than takes away...
  • testing new commenting system
  • Finally I fix the problems.Thanks you very much.It seems about the cache of the browser when I try to change many codes.So I clear the cache and I success.
    Futhermore, I want to translate it to Chinese.Would you mind I do it?
    .-= achi´s last blog ..My favorite high fashion eyeglasses =-.
  • I'm very glad you got it working, looks nice as well (sorry I can't read Chinese or I would check out your site more.

    I also would not mind if you translated this to Chinese as long as you provide a link back to the original post here.

    Take care and thanks for stopping by.
  • omgitsrfb
    I'm getting an infection error from clamxv with the js files associated with this jquery carousel you have recommended here. Is this an issue or something that is common?
  • some antivirus programs may have the occasional false positive but I never have had the issue with this script. I would recommend checking with the makers of clamxv on this one, this script comes from a reputable source and shows as being clean by every antivirus I have.
  • I am glad to see you share this article.But I have a quiction.I do not know why the positions of my fourth and fifth post is different from yours.
    May you help me to solve this problem? Thanks.
    .-= achi´s last blog ..穿街走巷,愛在天母 =-.
  • Thanks for your reply.I test this function in new clean blog.And it is good.You are right.There are conflicting css items on your page.But now I do not how to detect.I will retest on my page.
    .-= achi´s last blog ..My favorite high fashion eyeglasses =-.
  • Hi Achi,

    It looks like you have conflicting css items on your page, I would advise picking one element at a time in the featured.php file and try renaming them (and edit the corresponding css) to get rid of any conflicting entries..

    things like floats, padding, margins etc can also impact the layout quite a bit.. perhaps trying a clear after any floating elements on your page would help as well.
  • Ricky
    Say I'm using WPMU and wanted to get 5 posts from featured category from across the entire MU community? How the heck could I do that? Been trying but not sure I'm getting close. Thanks for the tutorial it works great for one blog.
  • Hey Ricky, Thanks for stopping by. If your looking for a great solution for what your trying to do (and quite likely a lot more) go sign up over at http://premium.wpmudev.org and grab their post indexer plugin to get all posts across all blogs into one db you can query easily, its really slick.

    They are by far the most helpful and knowledgeable people I have ever stumbled upon when it comes to WordPress MU.
  • Amy
    You're right - it's a float issue. Unfortunately, if I change the margins on the home page the other pages are messed up. I'll see if I can get some help to sort it out. I'm not much of a php person.

    Thanks for your help today.
  • Amy
    Not sure about the floats. I created the theme in artisteer, not from ground zero. I'll check.

    I've been thinking the problem might have something to do with where I'm placing the includes/featured.php code, but I don't know where else to put it.

    I decided to experiment and remove the code from the index.php and insert it into a sticky WP post. With the help of exec-php widget, this almost works.

    But, in a post the carousel images don't show up, nor does the text, but the list buttons (1-5), read more button, and place holders for the images show up. When I use the sticky post instead, the sidebar tab aligns perfectly, but the 1-5 list buttons take on the css of the theme and inserts bullets.

    Almost have it!

    I'll check the floats. - Thanks for the idea!
  • Amy
    And only 4 posts work, the 5th one (which doesn't necessarily correlate to button 5) is sometimes completely missing OR there is a bit of text to the left of the image. I noticed that when the image actually appears it's about 10 px to the right of where the other images fall, too.
  • Amy
    I resolved part of this by adjusting the margins. The read more button is 100% visible now.
  • I see the all 5 posts are showing now and you are getting closer with the layout stuff. Does your layout use any floats? If it does, you may need to use a clear both in there to get things back to normal..
  • Amy
    I thought that might be the problem too, so I put every thing down to around 500px, but it didn't help. The read more button is only showing the top half, too, so perhaps there's a clue in there. Hmmm....gotta love code mysteries!
  • Amy
    Spoke too soon - still having the issue with the sidebar tabs. If you have any thoughts I'd great appreciate it.
  • It looks to me like its still too wide and its causing the sidebar tabs section to drop down.

    Your site appears to have about 600px available in that area, the section for the posts slider that looks like this:

    #featured .jcarousel-container

    is still set to 620px and has 10px padding for an overall width of 630px. The best way to get this to fit just right is to just go down the list and take off an equal number of pixels for anything that has a defined width, start off by taking off 20px from everything then you can fine tune it from there. Hope this makes sense lol
  • Amy
    Hi Jesse - first, congratulations on the birth of your beautiful new daughter! How lucky she is to have 3 older brothers. :)

    I've been looking all over for a script like this one and am so grateful to have finally found your site.

    Like a few others, though, I'm having a few problems and would love your help if you have a moment (I realize you may be too busy...):

    www.lifeworkinaction.com

    Problems:
    -posts don't scroll
    -post buttons (1, 2, 3) don't show up
    -script pushes my Samsarin php widget with the sidebar tabs down so it's level with the first post (green tabs in right column)

    I'm using Chrome and just noticed that in IE the post buttons appear with a second "read more" button, but the buttons appear with the post that falls below the carousel.

    I have read and re-read your clear and easy to follow directions and am assuming I've done something wrong, but I can't for the life of me figure it out.

    Any help is greatly appreciated.

    Thanks & congrats once again.
    Amy
  • OK, i think I see what is causing it.. Your theme does not have jQuery included in it. I suppose I should have made a note that this was needed :-)

    You will want to add a line in your header.php file to include the jquery.js file thats located inside your wp-includes/js/jquery folder.

    is should look like this: *EDIT OK it wont let me put code here lol.. I will email it to you instead

    Please let me know if this sorts it all out
  • Amy
    Great, it's almost perfect. I think I just need to fiddle with the widths now.

    Thanks so much!!!
  • Hi Amy and thank you,

    I looked over your site and "hope" I can help you get it sorted :-) the first thing I noticed is the width, depending on how your site is set up you will probably have to specify the width for the #featured div in your css file (it's set at 100%) which works on my site, but everyones are a bit different. Also, the width of the other parts is based off the available width I have here, which is 636px so you will have to adjust that accordingly to get it just right for your site.

    The reason you see all the posts in IE is because the script for some reason isn't working (normally it will show the active bit and the rest are hidden until they scroll into view)I am looking into that for you now, I will post back on here in a bit with more information.
  • Rhys
    Very good tutorial, so easy to follow. The only problem I faced was forgetting to put posts in the featured category.

    Again, awesome tutorial!
  • Thanks, I am glad you found it useful. Please feel free to digg or stumble this post if you liked it :-)
  • farene
    good info.
  • Jeroen
    It also isn't working for me...

    I also don't understand the part:
    "and place the complete url to the image you wish you use, in the featured.php"

    Where should I put it?
  • The path for the image needs to be a custom field named thumbnail and the text that shows should be a custom field names feat_txt. You add these custom fields when you create or edit your post, its at the bottom of the edit post page.. ALso, can you please be more specific, leave a link to your site and let me know what it's not doing? Simply dropping by saying its not working doesn't give me enough information to help you.
  • Jeroen
    Thanks for the reply! i'm coding my theme locally, so I can't give you a link unfortunately.
    What I meant was that it says 'place the complete url to featured.php' but it is already there rigt? (get_post_meta($post->ID, 'thumbnail')

    When I just follow the steps here it simply isn't showing any output.

    I just figured it out!
    I put the featured.php in the main folder of my template and used in my header, and now it works...
  • Jeroen
    php include('feature.php');

    And you didn't mention to remove the <!--//--> for and after CData and /script.
    It is all functional now! Thanks!
  • Excellent, glad you got it sorted. please stop back by and give is a link to your site once you have it live :-)
  • this is just a test reply :-)
  • victor
    I've been looking for this exact thing, thanks. - Slight prob is I can't make it carousel. I've followed it to the t and no joy!?
  • If you can provide a bit more information I will be happy to help you get it going. What is the domain you have it on, does it show and just not slide automatically? Any more specific info you can provide will make it easier to sort out for ya.
blog comments powered by Disqus

"Awesome invention that will change the lives of people with disabilities, please spread the word http://rewalk.com (this is not an ad)"

7763