Easy and maintainable slideshows in Drupal
I received that request for most of my projects in the past year: the client wants a slideshow promoting either some of the content or the main features of the website. I have seen this implemented in many ways; from static Flash animations, to very dynamic, but very heavy, custom modules. I usually like to strip down the concept of a feature to its simplest form, then implement it the easiest way (usually a contributed module). In the end I just hack my way through a couple theme overrides and I'm done.
I am going to explain how to build a promotional slideshow (I will leave the featured content slideshow for a future blog post). The idea is that the client wants to be able to add a slideshow of pictures associated with some text that advertises the main features of his website: "Connect with 10,000 thousands professionals", "Manage your accounting online", "Share your videos easily with your friends"... The only thing is that the client is usually willing to update that information over time and, as far as I like receiving new work, this is not the kind of thing I would enjoy doing.
For those impatient to see what we're going to build
So the concept is simple: I want to have a customizable collection of elements gathering a picture and a tagline, being able to edit these elements and reorganize them in a sequence that will be displayed as a slideshow. One thing should pop in your head like a <blink> tag: Nodequeue. With it you'll be able to create sortable lists of nodes. Let's get started!
Create the Slide content type and the Slideshow nodequeue
First thing you want to do is to add a content type (admin/content/types/add): we'll use the name "Slide" and the type "slide". This content type should have an image field (you'll need the ImageField module for CCK). I named that field "field_slide_image". We will use the body as the tagline. If you use the Content Copy module (bundled with CCK), you can also directly use the following import files: download the import file for Drupal 5 or the import file for Drupal 6.
Now, let's add a simple nodequeue (admin/content/nodequeue/add/nodequeue) named "Slideshow" that is limited to the Slide content type. We'll then make sure to create a few Slide nodes that we'll add to that nodequeue.

That's it, we are now ready to deal with the appearance and behavior:
Theming and jQuery
First thing to do is to use ImageCache: we are going to make sure our pictures have the right size in the slideshow, whatever image has been uploaded. For this we create a new ImageCache preset: I personally will use a preset named "slideshow" that scale then crop to 350px (width) by 220px (height).

Time to theme our Slide nodes. Just create a node-slide.tpl.php file in your theme folder with the content:
<div id="node-<?php print $node->nid; ?>" class="slide">
<?php print theme('imagecache', 'slideshow', $node->field_slide_image[0]['filepath']); ?>
<? if (!empty($node->content['body']['#value'])): ?><div class="tagline"><?php print $node->content['body']['#value']; ?></div><?php endif; ?>
</div>
It will basically display the picture followed by the tagline (if tagline there is). Now, a bit of CSS for styling it:
.slide {
position: relative;
height: 220px;
}
.slide img {
border: 0;
margin: 0;
padding: 0;
}
.slide .tagline {
background: #000;
color: #fff;
filter: alpha(opacity = 75);
font-size: 14px;
opacity: 0.75;
position: absolute;
bottom: 0;
width: 350px;
}
.slide .tagline p {
margin: 0;
padding: 7px 10px;
}
This should give you something like this:

Now, we are going to add the following code where we want to display the slideshow:
<?php
drupal_add_js(path_to_theme() .'/slideshow.js');
print '<div class="slideshow">'. nodequeue_view_nodes(1) .'</div>';
?>
Updates:
- from the comments I got, it seems some people actually thought this code could be put into page.tpl.php, however this does not work. I actually tested it by putting the content above into a page node, but for people willing to put this directly into their source code, you could easily add the drupal_add_js() call into your template.php file.
- the Node Queue module has been updated and nodequeue_nodes() has been deprecated in favor of nodequeue_view_nodes() (see http://drupal.org/node/367571)
The nodequeue_view_nodes() function is part of the nodequeue API: this will simply display a list of node teasers for the nodequeue number 1 (this may be different for you if you have already created other nodequeues before the slideshow one). The slideshow.js is not yet there, so let's write it. I originally used Jon Raasch's slideshow script, but as simple as it already is, I consider it still too complex (to tell you the truth I spent 3 hours trying to debug it for one of my clients in Shanghai, and I hate wasting time). Here is my version of the slideshow script:
$(document).ready( function() {
// Every six seconds execute the switchSlide() function
setInterval( "switchSlide()", 6000);
});
// This function takes the first .slide element and put at the end
function switchSlide() {
var slide = $('.slideshow .slide:first');
slide.hide();
$('.slideshow').append(slide);
slide.fadeIn('slow');
}
Download the jQuery scriptWhich goes along with a couple additional CSS rules:
.slideshow {
height: 220px;
width: 350px;
margin: 0;
position: relative;
}
.slideshow .slide {
position: absolute;
}
Download the complete CSS fileJust make sure to have this jQuery script in your theme folder. This code will simply keep on taking the first slide of the queue and move it to the end; since each slide is position:absolute, only the latest one is displayed (the others are piled underneath). I added a little fadeIn() effect just for the fun of it, but you could put pretty much whatever you want: slideDown(), show()...
The result
If everything went smoothly, you should obtain something like this:
Pictures in Creative Commons from junipers
Now anybody should be able to create, add and sort slides in your slideshow.

- reply
Submitted by Anonymous (not verified) on Mon, 2009-01-26 04:08.Not that you should care-- but i just happened to be on a borrowed laptop that only had ie7, and the tagline is positioned at the bottom but to the right of the slideshow (like both were float: left).
Just though you'd want to know.
OMG i hate ie, lol.
- reply
Submitted by hunvreus on Mon, 2009-01-26 11:03.I actually do care about IE7, however my HDD died on my laptop recently and I haven't reinstalled Parallels to do cross-browser testing on IE. Will do this week, thanks for letting me know.
- reply
Submitted by Anonymous (not verified) on Thu, 2009-03-05 02:54.As a designer you might like to only work with 1 browser...I know I would...but...
Unfortunatly we have several, and IE is still the most popular in the bunch. If you are the only person looking at your site...then design for your favorite...but if you want the world to visit your door...then you have to design for all.
So hate it all you like...it is not going anywhere soon.
- reply
Submitted by hunvreus on Thu, 2009-03-05 07:14.Not sure exactly what is it that you try to tell me: I do care about IE (6 or 7) and always take them into consideration when developing websites. As I said before, I just had not take the time to reinstall a VM on my Mac after my hard drive died and could not do cross-browser testing...
- reply
Submitted by Anonymous (not verified) on Thu, 2009-03-05 09:47.my comment was not directed at you...it was directed at the original poster...
I usnderstand that you care...I saw your reply to him...
I was just pmsing at the other guy...lol
- reply
Submitted by appel (not verified) on Thu, 2009-09-17 18:56.I think this is due to a missing statement:
left:0;
ap
- reply
Submitted by Guy Saban (not verified) on Mon, 2009-01-26 05:58.Thanks for your slideshow post. It has helped me a lot.
I tried adding
<?phpdrupal_add_js(path_to_theme() .'/slideshow.js');
print '<div class="slideshow">'. nodequeue_nodes(1) .'</div>';
?>
to a page.tpl.php file but it did not work. Further research revealed that the drupal_add_js() function should be placed in a template.php file or implemented using another method that executes the function. Apparently the page.tpl.php file is to late in theme rendering to apply drupal_add_js().
In order to clear this up for other readers I recommend adding an editorial note to the post explaining where to add the necessary code.
Thanks again.
- reply
Submitted by Geoff (not verified) on Mon, 2009-01-26 06:00.Works as advertised.
- reply
Submitted by benway (not verified) on Mon, 2009-01-26 19:28.waitin for the featured content slideshow tutorial (:
- reply
Submitted by Drupalbased (not verified) on Fri, 2009-02-06 11:31.Yes please :) featured content slideshow would be great.
- reply
Submitted by dimitris (not verified) on Mon, 2009-01-26 21:53.Great simple idea and it works. I am really excited!
The only problem is that ie6 shows the tagline to the right of the image?
Any ideas to fix it?
- reply
Submitted by oyunlar (not verified) on Thu, 2009-03-26 16:32.I see also same bug. is there any fix?
- reply
Submitted by Per André (not verified) on Thu, 2009-01-29 20:43.Goodie! Tutorial starting now :)
- reply
Submitted by Chopi (not verified) on Thu, 2009-02-05 03:43.I updated NodeQ today and the slideshow broke, the function name was changed, see: http://drupal.org/node/367571
So change "nodequeue_nodes(1)" to "nodequeue_view_nodes(#)"
Great Tutorial btw!
- reply
Submitted by hunvreus on Tue, 2009-02-10 03:38.Thanks a lot for the comment: I updated the tutorial.
- reply
Submitted by Adrian (not verified) on Fri, 2009-02-06 14:15.code in node-slide-tpl.php not work in drupal 6
- reply
Submitted by Adrian (not verified) on Fri, 2009-02-06 15:41.After my testing, statement drupal_add_js(path_to_theme() .'/slideshow.js'); should put in template.php
- reply
Submitted by hunvreus on Tue, 2009-02-10 03:36.Thanks for the comment; tutorial updated.
- reply
Submitted by Ruby.qi (not verified) on Sat, 2009-02-21 22:54.thanks for your tutorial.
i saw you today,you are humour and enthusiasm
maybe i will translate this tutorial to my site ,can i ? haha.
- reply
Submitted by SendeGel.Org (not verified) on Thu, 2009-03-26 15:42.Thanks, good information
- reply
Submitted by siyah peynir (not verified) on Thu, 2009-03-26 15:53.code in node-slide-tpl.php not work in drupal 6
- reply
Submitted by adanalı (not verified) on Thu, 2009-03-26 16:45.Thanks, good information
- reply
Submitted by Dizi (not verified) on Fri, 2009-03-27 03:01.thanx. i'm a new durpal user.,i likde this ceode.
- reply
Submitted by id_ivan (not verified) on Mon, 2009-04-06 17:53.tagline stil rendered on the right side of image instead of inline (IE 7).
Works perfect in FF though
- reply
Submitted by sklep rowerowy (not verified) on Tue, 2009-08-11 23:03.Thanks
- reply
Submitted by dhassan (not verified) on Wed, 2009-04-22 00:19.Thanks for the script - it's just what I needed!
Got a question, though. My images are large (100-200k each) and I get more than just a flicker - I either see each of the images flash as it loads, or it takes even longer and I get a little preview of the entire slideshow, instead of a clean initial playthrough. I noticed that the Jon Raasch example you cited uses CSS to set the opacity to 0 and only the active image to 1, but I'm having trouble implementing the same functionality in this script.
Can anyone help me out? Much appreciated.
- reply
Submitted by Anran (not verified) on Sat, 2009-07-04 01:16.Sorry but I can't see my Slideshow anywhere. It is probably my fault because instead of adding content type 'slide', I added existing content type 'images' into the nodequeues. What if someone wants to use existing contents for the slideshow? Thanks!
- reply
Submitted by khaki (not verified) on Mon, 2009-07-13 01:51.hello... is it possible to add pager? i have a thread http://drupal.org/node/507988 that still haven't solved :(
- reply
Submitted by Binyamin (not verified) on Fri, 2009-07-31 14:43.Texts in Slideshow shows one under other, they do not have opacity 0.0
Some resolution?
http://jonraasch.com/blog/a-simple-jquery-slideshow#comment-1174
- reply
Submitted by Binyamin (not verified) on Mon, 2009-08-03 17:12.Please give me some sample for this slideshow code where it's working fine with texts. Here slideshow shows text under the text in opacity: 0.75;
- reply
Submitted by takpar (not verified) on Wed, 2009-08-26 01:31.Hi,
very thanks for your article and the idea you shared.
i used jquery cycle plugin to rotate images,
i added a new block and put it content like:
<?php
// Jquery Plugins module
jquery_plugin_add('cycle');
?>
<?php
echo nodequeue_view_nodes(1);
?>
$(document).ready(function() {
$(".cycle").cycle();
});
- reply
Submitted by M Adam (not verified) on Tue, 2009-10-06 19:45.Sorry I wrongly said in my earlier message abt field name.. I created fieldname as u said .. (label: slide_image; name: field_slide_image File Configure Remove). ANd also had forgot to mention that I had put css in local.css file while anabling it in dot info file. After doing all this .. it is still not clear where to put CODE in template.php file and why is it showing parsing error when I put it when it is working well for others. Plz help me understand and do thing rightly. I willbe obliged.
- reply
Submitted by Manu Adam (not verified) on Thu, 2009-10-08 01:00.Hello Sir,
Plz help me as I have tried many times but failed. It is not working on my site. First, I am using acqua_marina theme. I followed all your steps and did accordingly. I first created a content type Slide. Added the field (field_slide_image) in mange field and saved it. Then i created a nodequeue Slideshow . Then i went to create content and created slide1, added it (with nothing in body and just uploaded an image). After that I clicked at the above tab of nodequeue and added this slide to nodequeue. Similarly I created other three slides. Then I went back again to Nodequeue .. there all these slides are showing well .. slide1,slide2, slide3. After that I creeated slideshow.js file and put that in theme folder. Then created a node-slide.tpl.php file and saved it too in theme folder. Then I created imagecache preset slideshow and saved it.I put css in local.css file and enabled it in dot info folder.
After all this I saw that a block has been created automatically with name queue Slideshow .. I enabled that block and saved it for the COntent Top region.
After saving it It just shows the name QUEUE SLIDESHOW and a text list, listing all the three slides.
Then I added that ... code php
drupal_add_js(path_to_theme() .'/slideshow.js');
print ''. nodequeue_view_nodes(1) .'';
..
as it is at the end of my theme's template.php file.
When I saved it .. Page started showing parsing error. . Then I stripped that code of its <?php element and tried to put..again it didnt work.
Please tellme where exactly to put this code and why is it not working when I am putting it as it is in my theme's template.php file.
I also tried to create another block and put that code in the body with php input type and saved that. But that block also didnt work. Please help me. Thanks.
- reply
Submitted by mcdazz (not verified) on Sun, 2009-10-18 15:04.Firstly, a great article and one that I'm sure will help many people.
I have tested this in both IE7 and Firefox 3.0.11 and for some reason it doesn't work in Firefox while it does work in IE7.
I'm guessing this has something to do with the javascript but I'm not exactly sure what.
Anyone else come across this and/or fixed it?
Cheers,
Dazz
- reply
Submitted by mcdazz (not verified) on Sun, 2009-10-18 15:07.Wouldn't you know it - straight after posting my comment, it started working in Firefox.
Apparently it doesn't want to work when being stared at. ;-)
- reply
Submitted by timur (not verified) on Mon, 2010-10-11 06:39.çok güzel bir site teşekkürler
- reply
Submitted by fatmagulun sucu ne izle (not verified) on Mon, 2010-10-11 06:41.çok güzel bir site teşekkürler