Wordpress Featured Post Slideshow
This post over at NETUTS a long time ago received a lot of comments that it was not animated. I thought it would be neat if I could figure out a way to animate it and now several months later, when I was working on the new theme I got around to it. I’ll show you how to
- use wp_enqueue_script to load the Jquery that is packaged with Wordpress.
- go over how Jquery Cycle works and make The Loop generate the necessary XHTML
- make a custom query to load only the posts we want
- configure Jquery Cycle to run the slideshow
1. Enqueue Jquery
The first thing you want to do is to get Jquery to load Wordpress comes with a version of Jquery included so its generally best to load that> The function to load javascript libraries and plugins is wp_enqueue-script. The code to load the jQuery and the jQuery Cycle plugin is below
<?php wp_enqueue_script('jquery.cycle.all.min','/wp-content/themes/upsidedowncity_v_3/jquery.cycle.all.min.js',array('jquery')); ?> <?php wp_enqueue_script('jquery_script','/wp-content/themes/upsidedowncity_v_3/jquery_script.js'); ?> <?php wp_head(); ?>
You place this code right above the “wp_head();” line your header.php file. The first wp_enqueue_script calls JQuery cycle which has been uploaded in to my current theme folder. The first section is the name of the script (file name minus the .js) then the path to the script then the array holds the libraries that the script depends on (JQuery in this case).
The next wp_enqueue_script calles “jquery_script” which is the .js file where we can place all of our jquery.
How Jquery Cycle Works
Jquery Cycle is a jquery plugin build for slideshows. To make it work you give it an element and it turns every child of that element into a slide. so to make our slideshow work we need to have a div with some children and in those children will be out images and headers.
So we need to make a containing div with unordered lists that hold 3 images and links. These ULs are the “slides ” Not the LIs.
The Code
Don’t get overwhelmed by the next code block. I’ll go over it.
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 | <div id="featured_posts" class="clear"> <a class="prev"></a> <div class="slide_cont"> <?php $containter = 1; $display = 1; ?> <?php $my_query = new WP_Query('category_name=featured'); while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php if (($containter == 1) and ($display >= 4)) { echo "<ul class='slides' style='display:none;'>"; } elseif ($containter == 1) { echo "<ul class='slides'>"; } else {} ?> <li> <h3 class="long"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3> <a href="<?php the_permalink() ?>"><img width="260" height="120" src="<?php $values = get_post_custom_values('featured'); echo $values[0]; ?>" alt='<?php the_title(); ?> Thumbnail Image' /></a> </li> <?php $containter++; $display++; ?> <?php if ($containter >= 4) { echo "</ul>"; $containter = 1;} else {} ?> <?php endwhile; ?> </div> <!-- end #slide_cont --> <a class="next"></a> </div> <!-- end #featured_posts --> |
Right, Starting at the top on line 1 we begin a div to wrap this whole section with, we also end this div on line 41.
On line 2 we make an empty anchor tag and give it a class of “prev” this will be the button that make the slideshow go to the previous slide. There is also another anchor with a class of “next” right before the featured_posts div closes.
Lines 6-9 make 2 php variables called “container” and “display” later we will use these variables to control when to make our slide containers.
11-12 creates a new wp_query and tells to get everything from the category called “featured”. If you want your slides to be from a different category you can change that here. This also starts The Loop and everything from here until line 37 will repeat for every post in the “featured” category.
14 15 16 17 18 19 20 | <?php if (($containter == 1) and ($display >= 4)) { echo "<ul class='slides' style='display:none;'>"; } elseif ($containter == 1) { echo "<ul class='slides'>"; } else {} ?> |
This is code that generates the wrappers around our slides. it uses the variables from lines 6-9. Lines 15-16 say if container is equal to 1 and display is equal to or greater then 4, make an unordered list with “display:none;”. You need to have display:none; because otherwise users will see all the slides while the document is still loading and then they all get hidden.
The next 2 lines say if the above test fails (i.e. container is not equal to 1 and display is not equal to or greater than 4) to check if container if equal to 1 if it is them make an unordered list.
If both these test fail nothing happens.
Lines 22-25 generates the header and the images for each post. You can add the images inthe custom field section in the wordpress write panel by putting a path to image as the value and the key as “banner”. For more information you can visit
The PHP chunk on lines 27-30 tells php to add 1 to the value of both of out variables you will see why later.
32 33 34 35 | <?php if ($containter >= 4) { echo "</ul>"; $containter = 1;} else {} ?> |
The above php checks the “counter” variable and if it is equal to or greater then 4 it ends the unordered list that was started above when the UL was started. Then it resets container to 1 so a new list will start next time we go through the loop. If counter is not equal to or greater then 4 it does nothing.
How it works
Here is a visual flow for how the above loop should work.
- The Loop Starts.
- The first “if” statement fires counter is equal to 1 so a visible UL is made.
- An image and link are generated
- Display and container now equal 2
- the second “if” statement is checked container equal 2 so nothing happens
- The first “if” statement fires container and display equal 2 so nothing happens
- Another image and link is made
- Display and container now equal 3
- the second “if” statement is checked container equal 3 so nothing happens
- The first “if” statement fires container and display equal 3 so nothing happens
- Another image and link is made
- Display and container now equal 4
- the second “if” statement is checked container equal 4 so our first 3 links and images are wrapped in a UL. Counter now equals 1
- The first “if” statement fires container equals 1 and display equals 4 so a hidden UL is made
- An image and link are generated
- Display equals 5 and container equals 2
- Ect…
What we have now is a DIV that contains unordered lists. each list contains a set of three posts
Make Cycle Work With Our Loop
Now we have our loop configured and it is generating XHTML like the following.
<div class="slide_cont"> <ul> <li><!--Loop Stuff --></li> <li><!--Loop Stuff --></li> <li><!--Loop Stuff --></li> </ul> <ul> <li><!--Loop Stuff --></li> <li><!--Loop Stuff --></li> <li><!--Loop Stuff --></li> </ul> </div>
Now in the jquery_script.js file in out theme folder we can insert the following example from the Cycle documentation.
jQuery(document).ready(function($) { $('.slide_cont').cycle({ fx: 'scrollDown' }); });
Notice that I did not call JQuery with the $ shortcut. By default the jQuery included with wordpress uses jQuery.noConflict( ) so it does not interfere with the Prototype library. Hence using the different (document).ready(function) is needed. You can read more about noConflict here.
Note I use a slightly different cycle on my web page that adds previous and next controls. and adds several other parameters. I also use CSS buttons to make those empty anchors in the previous and next buttons.
Style Your Slideshow
Cycle works best when you fix all of your elements widths and heights. Below are the styles that I have applied to my sideshow
/*Featured Post Slide Show*/ #featured_posts{ width:960px; margin-left:-10px; height:140px; } #featured_posts h3{ font-size:12px; width:255px; } #featured_posts ul{ } #featured_posts ul li{ float:left; margin:0 9px 0; } .slide_cont{ width:835px; clear:none; float:left; margin:0 17px; } .slide_cont ul li h3 a, .slide_cont ul li h3 a:hover{ color:#fff; } .prev{ display:block; width:35px; height:75px; background: url('images/left_arrow.png') no-repeat center; cursor:pointer; float:left; margin:35px 0 0 10px; } .next{ display:block; width:35px; height:75px; background: url('images/right_arrow.png') no-repeat center; cursor:pointer; float:right; margin:35px 10px 0 0; } h1, h2, h3, h4, h5, h6{ color:#fff; background:#000; font-family:"courier new",courier; padding:3px 0 3px 5px; }
That should give you some basic styles to get you started.
Bring it all together
This is where it all comes together. I made my life easy by grouping all of the PHP code for the slideshow in featured_post_slideshow.php and used
<?php include('featured_posts_slideshow.php') ?> <!--Adds Featured Posts -->
At the bottom of my header.php to make it work across every page. Also now would be a good time to check if you have Cycle and jquery_script.js in your theme directory and is the wp_enqueue-script calls are correct and placed above the wp_head(); line in your header.php
If you have any questions leave a comment and I’ll help you out.
This also has some bugs. If the number of posts in your “featured” category isn’t evenly divisible by 3 the whole thing will come crashing to a halt.
Includes featued_posts_slideshow.php, CSS, Images and JQuery.


















Chad
October 22, 2008 at 8:09 am
I would have liked to have read this but not with that horribly distracting background.
admin
October 22, 2008 at 8:23 am
Your the second person who has commented on the background. If I change it I’ll be sure to tell you.
21 Most Wanted And Essential Resources Specially If You Are Developer - Opensource, Free and Useful Online Resources for Designers and Developers
October 22, 2008 at 6:34 pm
[...] Wordpress Featured Post Slideshow [...]
21 Most Useful Free Resources For Designers And Web Developers | Wordpress Blog Services
October 24, 2008 at 4:28 pm
[...] Wordpress Featured Post Slideshow [...]
21 Most Useful Free Resources For Designers And Web Developers
October 24, 2008 at 9:16 pm
[...] Wordpress Featured Post Slideshow [...]
21 Most Useful Free Resources For Designers And Web Developers | SulVision
November 3, 2008 at 2:47 am
[...] Wordpress Featured Post Slideshow [...]
federico
November 17, 2008 at 6:47 am
hi. very itneresting tutorial. i’m planning to use it on one of my sites. i have only one question: is there a way to run the slideshow automatically? thanks in advance.
kaiser
November 20, 2008 at 4:19 am
Hi,
it´s always hard to imagine… couldn´t you provide a demo?
thanks a lot
pab
January 5, 2009 at 1:13 pm
Nicely done, I adapted the code to work with the s3slider jquery plugin
thanks for the inspiration.
pab
Jill
January 10, 2009 at 10:56 pm
Hi, I am using this and it works great, but it only will display and let me scroll through TEN POSTS. How can I modify this to at least allow for 12?
Thanks!
jill
January 11, 2009 at 8:35 am
Hi,
Question – I had this working on my site, and then it just stopped working out of nowhere. I didn’t change a thing. Do you have any idea what could have caused that to happen?
Thanks
-Jill
Using jquery s3slider to display featured post in wordpress with no plugin | pab's corner
January 18, 2009 at 5:37 pm
[...] Upsidowncity [...]
irrelevant
January 26, 2009 at 9:05 am
when you’re done animating, perhaps you can fix the spelling of “featuring”
The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials
January 26, 2009 at 11:06 am
[...] 22. Wordpress Featured Post Slideshow [...]
Wordpress Blog Services - The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials
January 26, 2009 at 12:48 pm
[...] 22. Wordpress Featured Post Slideshow [...]
The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials | @article @lbum
January 26, 2009 at 10:21 pm
[...] 22. Wordpress Featured Post Slideshow [...]
WPCult
February 2, 2009 at 1:12 pm
This a a great tutorial, and a very nice script.
The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials | Web2.0
February 3, 2009 at 1:11 am
[...] 22. Wordpress Featured Post Slideshow [...]
admin
February 3, 2009 at 12:59 pm
Thanks!
Free WordPress and jQuery: 25+ Useful Plugins & Tutorials | guidesigner.com
February 17, 2009 at 12:52 am
[...] 22. Wordpress Featured Post Slideshow [...]
rob
March 7, 2009 at 11:05 am
can you show us a demo please?
admin
March 8, 2009 at 12:16 pm
The slideshow at the top of the page is actually this exact code. I’ll have to make some demo code though as everyone seems to ask this.
TASARIMWEB
March 13, 2009 at 5:21 am
good job.. thanks
Germz
March 22, 2009 at 3:37 pm
I’m kinda lost, in the calls you are calling for 2 files which don’t exist
”
“
Avinash
March 22, 2009 at 10:40 pm
nice work, was able to get it working but would like to ask how to get 5 posts to display in each slide by reducing each slide size ie., the image and title width.
thanks
admin
March 26, 2009 at 9:47 pm
You should be able to adjust the PHP part of the code to accommodate for 5 posts at a time. $display >= 6 and if ($containter >= 6) should do the trick.
admin
March 26, 2009 at 9:50 pm
The jquery files i am calling are bundled with Wordpress or you just need to place them in your theme folder. they are other peoples code that I am using for this effect.
Nick
March 31, 2009 at 3:50 pm
Hey, if I wanted to modify this so that it scrolls 1 at a time instead of 3, how would I do it?
woodman
April 1, 2009 at 6:32 am
It is a good article,thanks for your sharing.
Free WordPress and jQuery: 25+ Useful Plugins & Tutorials | designersmantra.com
April 4, 2009 at 9:17 am
[...] 22. Wordpress Featured Post Slideshow [...]
bedding sets
April 5, 2009 at 9:08 am
It is good information!
Argonaut
April 19, 2009 at 2:29 pm
Hi!!
Thanks for the script! :)
Is there a way to show more than 10 posts?
thanks
A
3 Reasons Why You Should Use Featured Post Section on Your Blog | Best Wordpress Themes, Blogspot Templates and Make Money Online @ BLogicThink [dot] com
June 26, 2009 at 2:54 am
[...] Wordpress Featured Post Slideshow [...]
The Power of WordPress and jQuery: 25+ Useful Plugins & Tutorials | Quest For News, A TUTORIAL Base
June 27, 2009 at 10:00 am
[...] 22. Wordpress Featured Post Slideshow [...]
Disha Hosting » jQuery Wordpress Plugins Strikes Again
August 4, 2009 at 3:56 am
[...] Wordpress Featured Post Slideshow Plugin. This plugin allows you to create animated slideshows, which should be eye catching if nothing else. [...]
30 Tutorials Combining Both Wordpress and jQuery : Speckyboy Design Magazine
August 6, 2009 at 9:08 am
[...] Wordpress Featured Post Slideshow [...]
Lista de Tutoriais combinando JQuery e Wordpress: turbine seu blog | Blog Tecnologia e Educação
August 7, 2009 at 11:51 am
[...] Este é um tutorial para animar artigos em destaque como um slideshow. [...]
30 Tutorials Combining Both Wordpress and jQuery | huibit05.com
August 8, 2009 at 7:53 pm
[...] Wordpress Featured Post Slideshow [...]
Marcus
August 15, 2009 at 11:50 pm
Hi,I tried this slideshow code in my new web, but it fail to show the picture, only a failed pic take the place, where can I fix it?
Your soonest reply is highly appreciated!
Marcus
2009/8/16
admin
August 16, 2009 at 9:26 pm
You need to define the image in the custom fields section of your Wordpress post otherwise you will get nothing.
30 Tutorials Combining Both Wordpress and jQuery | .::tek3D Weblog::.
August 18, 2009 at 5:33 am
[...] Wordpress Featured Post Slideshow [...]
will
August 18, 2009 at 6:22 am
demo??
Featured Posts Plugin
August 21, 2009 at 1:48 pm
I am using the Featured Articles wordpress plugin from coolwebdeveloper.com, and luvv it so far.
It provides ability to change themes, add/remove articles automatically.
You can try it here – http://www.coolwebdeveloper.com/plugins
francis
August 26, 2009 at 1:51 pm
what am i doing wrong?
when i try tu unzip the file, some error message shows up about CANNOT OPEN FILE
looks all files where unzipd succesfulled, but i cant see the plugin on my dashboard
abhi
September 22, 2009 at 8:15 pm
Thanks for the code, you have done it nicely.
Daily Digest for September 24th
September 24, 2009 at 6:45 am
[...] Wordpress Featured Post Slideshow | Upside Down City [...]
30 Tutorials Combining Both jQuery and Wordpress
September 26, 2009 at 1:11 am
[...] Wordpress Featured Post Slideshow [...]
30 Tutorials for Using JQuery in Wordpress | oOrch Blog
September 27, 2009 at 8:52 am
[...] Wordpress Featured Post Slideshow [...]
40+ Quite Useful Wordpress Plugins using jQuery | tripwire magazine
October 6, 2009 at 1:03 pm
[...] Wordpress Featured Post Slideshow [...]
Ganhe dinheiro blogando
October 21, 2009 at 1:22 pm
Hey, man, it is a pretty plugin for wordpress, I am going to try it now, thanks
秋天的博客 » 18个Wordpress jQuery插件
November 11, 2009 at 6:52 am
[...] 18. Wordpress Featured Post Slideshow [...]
Seth
November 22, 2009 at 8:44 pm
This maybe a simple question, but is there a way to limit the characters to use in the title so it doesn’t go to 2 lines? That way each post are aligned and not off by a line?
Seth
November 22, 2009 at 8:54 pm
Never mind, found an answer. Here it is if someone is looking for the same: if(strlen($post->post_title)>5){echo substr($post->post_title,0,5).’…’;}else{the_title();}
Replace the 5’s with your own number.
WarDooccack
November 25, 2009 at 7:41 am
I’m always looking for brand-new blogposts in the net about this issue. Thanx.