I finally finished getting my new custom jQuery featured post slider working as you can see in the header of the site. It took some trial and error but I eventually managed to figure out how to get it working. I ran into issues because I was first trying to use query_post() to get the posts, but then learned about WP_Query() and that solved all my problems. This is a better type of banner rotator than my previous Php and JavaScript banner rotator posts too. I would recommend using this one instead.
Now that I finally got it working, I am going to start working on making it a plugin for WordPress that I can release if I ever finish it, or can even manage to learn how to develop a WP plugin for that matter. I’m still rusty with Php and the development side of things, but I will do my best to create a plugin to release.
Since I had a hard time in finding information on how to do this, I wanted to share my code. What it does is dynamically display posts from the ‘Featured’ category, using the easySlider jQuery plugin. It’s easy to do, and adds a nice animated and dynamic feature to your site.
What you will learn:
- using the WordPress WP_Query() funtion
- retrieve posts from a specific category
- display post titles and excertps in jQuery slider
Getting started
For this you will need to download the jQuery easySlider plugin. You will also have to be able to edit the header.php template for your theme. Go here for some information about using WP_Query() function here.
For the latest example usage of easySlider with just static content you can download the files. I used the example from that, and just altered the CSS and made the list of content dynamic instead of static, so you may want to start from there since my CSS is different.
These are the arrow images I used for my slider. Feel Free to save them.

Here is the CSS styles I have.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #slider{ margin: 0px 25px; height: 180px; overflow: hidden; } #slider ul, #slider li { margin:0px; padding:0; list-style:none; } #slider li { /* define width and height of list item (slide) entire slider area will adjust according to the parameters provided here */ width:500px; height:180px; overflow:hidden; } #slider li h2{ margin:0px 20px; padding-top:10px; } #slider li p{ margin: 5px 20px; } p#controls { margin:0; position:relative; } #prevBtn, #nextBtn { display:block; margin:0; overflow:hidden; text-indent:-8000px; width:18px; height:30px; position:absolute; left:5px; top:-100px; } #nextBtn { left:525px; } #prevBtn a, #nextBtn a { display:block; width:19px; height:30px; background:url(images/btn_prev.png) no-repeat 0 0; } #nextBtn a { background:url(images/btn_next.png) no-repeat 0 0; } |
Next you need to add this before the closing head tag head> in your header.php template, which inlcudes the external JavaScript files. Change the path to the files according to where you save them.
1 2 3 4 5 6 7 8 9 10 11 12 | <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/easySlider1.5.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#slider").easySlider({ controlsBefore: '<p id="controls">', controlsAfter: '</p>', auto: true, continuous: true }); }); </script> |
Now for the fun part.
This works in a similar way as the query_post() function does but that was conflicting with the WordPress loop and making all the posts on my site to ONLY display the posts in featured category. This creates a new instance of query_post(), and allows you to do various things while within The Loop.
First you need to open the div and unordered list.
Then you need to set up the query and get the number of posts from the category you want to get. Then create the loop.
1 2 3 4 5 | <?php $featuredPosts = new WP_Query(); $featuredPosts->query('showposts=5&cat=7'); while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?> |
In this example, showposts=5 will get 5 posts, and cat=7 is getting the posts from the category with the ID 7. If you can’s find the category ID, go to Categories in your WP admin, and hover over the category links. You will be able to see what the link is in your browser window, and the end of the URL will say &cat_ID=X.
Next, display the list of posts. This will show the post title with using the_permalink() and the_title() functions, and the_excerpt() to display a short summary of the post. You would have to make sure that all posts in the Featured category have a summary in the Excerpt field when you create new posts.
1 2 3 4 | <li> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <div class="postsnip"><?php the_excerpt(); ?></div> </li> |
Close the loop. Add the closing list and div tags and that’s it!
1 2 3 | <?php endwhile; ?><!--/close loop--> </ul> </div><!--/slider--> |
If it were a perfect world, this would work for you on your own site just by copying and pasting, but if you have ever developed anything before, you know it’s never that simple. If you have any questions or issues with this let me know.
Here is the entire code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <style type="text/css"> #slider{ margin: 0px 25px; height: 180px; overflow: hidden; } #slider ul, #slider li, #slider2 ul, #slider2 li{ margin:0px; padding:0; list-style:none; } #slider li, #slider2 li{ /* define width and height of list item (slide) entire slider area will adjust according to the parameters provided here */ width:500px; height:180px; overflow:hidden; } #slider2 li{ background:#f1f1f1; } #slider li h2{ margin:0px 20px; padding-top:10px; } #slider li p{ margin: 5px 20px; } p#controls, p#controls2{ margin:0; position:relative; } #prevBtn, #nextBtn, #prevBtn2, #nextBtn2{ display:block; margin:0; overflow:hidden; text-indent:-8000px; width:18px; height:30px; position:absolute; left:5px; top:-100px; } #nextBtn, #nextBtn2{ left:525px; } #prevBtn a, #nextBtn a, #prevBtn2 a, #nextBtn2 a{ display:block; width:19px; height:30px; background:url(images/btn_prev.png) no-repeat 0 0; } #nextBtn a, #nextBtn2 a{ background:url(images/btn_next.png) no-repeat 0 0; } </style> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/easySlider1.5.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#slider").easySlider({ controlsBefore: '<p id="controls">', controlsAfter: '</p>', auto: true, continuous: true }); }); </script> <div id="slider"> <ul> <?php $featuredPosts = new WP_Query(); $featuredPosts->query('showposts=5&cat=7'); while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?> <li> <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2> <div class="postsnip"><?php the_excerpt(); ?></div> </li> <?php endwhile; ?><!--/close loop--> </ul> </div><!--/slider--> |
If you want to see a demo of this, just look at the top of this site, I am using almost this exact code to display the featured post slider in my header. So it shouldn’t be too difficult to implement yourself. There’s always a bump in the road though.
Questions or comments? Post them below.







Everyone gets sick of all the thousands of advertisements seen on the internet, on just about every website you visit, at any given time. Some advertisements are so annoying and sometimes you just want them gone. You may be one of the 58 million Firefox users that have installed the extension,
I recently came to learn how useful using the excerpt feature that is built into Wordpress. You can use excerpts if you want to show a short blurb for each post on your blog’s homepage, which I have been wanting to do on Tweeaks for a long time now I just didn’t know how to do it. Since I was doing some layout modifications, and tweaks FINALLY, I figured I should find out how to use excerpts, and I am happy I did. It’s so easy


















December 28th, 2009 at 1:32 pm
RT @tweeaks How To Make A Dynamic jQuery Feature Post Slider For WordPress http://bit.ly/5EZExv
December 28th, 2009 at 1:33 pm
Looks great Jared. Appreciate your hard work, and sharing it man!
December 28th, 2009 at 1:33 pm
Mark Essel: RT @tweeaks How To Make A Dynamic jQuery Feature Post Slider For WordPress http://bit.ly/5EZExv: http://bit.ly/4FlJka
December 28th, 2009 at 2:21 pm
No problem. I'm glad you like it
December 30th, 2009 at 12:47 pm
"How To Make A Dynamic jQuery Feature Post Slider For WordPress" #tech #jquery http://j.mp/8uxkzu
December 30th, 2009 at 12:51 pm
Thanks so much! This is exactly what I was looking for!
December 30th, 2009 at 12:52 pm
"How To Make A Dynamic jQuery Feature Post Slider For WordPress" #tech #jquery http://j.mp/8uxkzu (via @elijahmanor)
December 30th, 2009 at 12:59 pm
Your welcome. I looked for something like this too, it's hard to find.
December 30th, 2009 at 3:18 pm
http://tinyurl.com/y8tgqtt
How To Make A Dynamic jQuery Feature Post Slider For WordPress | Tweeaks Design
December 30th, 2009 at 6:57 pm
"How To Make A Dynamic jQuery Feature Post Slider For WordPress" #tech #jquery http://j.mp/8uxkzu via @elijahmanor
December 30th, 2009 at 7:00 pm
RT @JungchanHsieh: "How To Make A Dynamic jQuery Feature Post Slider For WordPress" #tech #jquery http://j.mp/8uxkzu via @elijahmanor
December 31st, 2009 at 1:30 am
"How To Make A Dynamic jQuery Feature Post Slider For WordPress" #tech #jquery http://j.mp/8uxkzu @elijahmanor
January 7th, 2010 at 5:26 am
Thanks for the great tutorial. Do you know perhaps if there is a way to control how much time it stays on a certain post until it slides to the next one?
January 7th, 2010 at 7:46 am
You can control the speed, pause length, vertical/horizontal, and a number of other things in really easy, in the easySlider.js file.
It's really easy, and self-explainatory really.
January 7th, 2010 at 6:20 pm
RT @hashwordpress: RT @ruhanirabin How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 7th, 2010 at 11:12 pm
How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 7th, 2010 at 11:20 pm
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 15th, 2010 at 9:36 pm
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 21st, 2010 at 6:08 am
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 21st, 2010 at 6:09 am
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 21st, 2010 at 11:10 am
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 21st, 2010 at 11:18 am
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 21st, 2010 at 12:47 pm
RT @ruhanirabin: How To Make A Dynamic #jQuery Feature Post Slider For #Wordpress http://j.mp/5qPyoB
January 24th, 2010 at 6:26 pm
Good article...I will use some of these interesting principles myself...more good info please...
February 24th, 2010 at 2:07 am
This is great, except that I'm using the same code as you, but my slides don't keep cycling the way that yours do. When I reach the last slide, my next button disappears, whereas on your site, the posts just cycle back to the beginning. How did you do this?
February 24th, 2010 at 6:07 am
You want to add this...
[php]continuous: true[/php]
This is the code I have in the head of my site.
[php]
$(document).ready(function(){
$("#slider").easySlider({
controlsBefore:'<p id="controls">', controlsAfter: '',
auto: true,
continuous: true
});
});
[/php]
February 27th, 2010 at 5:06 pm
First i tried your slider script from new2wp. There i had troubles with the first for (while) loop, which never stopped and threw out #slider-infinity. Then i found this one right here, seemed to be a little easier, but still doesn't work. It prompts the first excerpt but does not display anything more or is moving content by time. http://www.onemonkey.de/www/moritzschefers
Checked all the php jQuery css with by skills but can't find the mistake. The source-code should display the
Could send code by mail, don't want to mess up these comments. Answer would be very pleasing.
Thank you very much either way for sharing your skills.
March 2nd, 2010 at 5:33 pm
Hmmm. Nice tutorial. How can we use numbers instead of arrow image? All I want to have is a small numbers like 1,2,3,4,5 instead of arrow image at the bottom of the slider so that the people can use it easily. A good example of it is smooth slider plugin.
March 2nd, 2010 at 1:04 pm
Yes you can, in this part you would just add numeric: true
$("#slider").easySlider({
controlsBefore: '',
controlsAfter: '',
auto: true,
continuous: true
You can find all the options for the easySlider plugin here
http://cssglobe.com/post/5780/easy-slider-17-numeric-navigation-jquery-slider
March 2nd, 2010 at 1:13 pm
Thanks for the quick reply. You are really helpful. I'm going to add it to the next version of my theme. You can check my theme at above link. I'm not living a link here as you may feel like I'm spamming. My theme name is simplegant theme.
March 2nd, 2010 at 1:21 pm
Cool, looks like a nice theme. If you have any trouble adding the slider let me know.
March 8th, 2010 at 3:40 am
Done. Thanks for the help. While defining a javascript in header file it should be easySlider.js not easySlider1.5.js
June 29th, 2010 at 4:20 pm
We recently built a dynamic WordPress slider. It's very similar in functionality to what Jared has described. However, it doesn't require any coding and can be ready to go in a few seconds. Just pick a template and your post criteria (recent, popular, featured) and you are good to go.
Sorry for the links, but it's the best way to see it in action:
Example: http://www.dtelepathy.com/blog/
Download: http://www.slidedeck.com/wordpress
July 26th, 2010 at 2:19 pm
how to make to show two posts at once? thanks
July 27th, 2010 at 8:52 am
Hi , Thanks for the tutorial very easy to implement with a little jiggling here and there , I am not very good with php, jQuery but was excited to see how easy this was to follow.
My only problem was how to bring the images in as well?
I'll have a look around but if you manage to reply that'd be cool.
Thanks for your time.
P
July 27th, 2010 at 2:01 pm
What I did in this was just include the image in the excerpt. so you do something like:
<img src="blah" />The excerpt text here.
July 27th, 2010 at 2:34 pm
I gave the image a custom field and wrote a wp_query
sweet,
I enjoyed your tut's , thanks man its a good feeling when things come together.
phill