Getting jQuery Nivo Slider To Work Inside Your WordPress Theme

/ Javascript, jQuery, Wordpress / by Paul Robinson / 275 Comments
This post was published back on September 29, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

Adding Nivo slider to a WordPress theme is a lot easier than adding any other slider I have worked with. It may seem as if there are a lot of steps to it, but it’s fairly easy to follow. Let’s get straight into it.

Yvonne Strahovski & Zachary Levi. Picture © NBC Studios. No infringement intended, used for illustrative purposes only.

Downloading Nivo Slider

Obviously the first thing you need to do is download Nivo Slider. Just click the download button, production is the best option, then a little above that there is a header that says “Style Your Slider Like The One On This Site”. For ease of use we are going to use the style pack provided so click the button to download that & extract both into a folder called ‘js’ inside your theme’s folder.

Installing Nivo Slider

I use the term installing loosely as we are really just including the scripts. A lot of users tend to just hard code script tags into the page & while you can do that, I don’t recommend it. Before we get into any of that though we need to check to your theme to see if it already includes jQuery or not. If you are developing from scratch it probably won’t, if you are adding Nivo to an existing theme open up your front page & check your source code for a script tag including jQuery.

Now that we have determined whether or not you have jQuery included or not you can alter the code as needed. I’ll add a comment next to the line to remove if it is already included.

You will need to add this to your themes functions.php. If your theme doesn’t have one you will need to create it. All we are doing is using a built in WordPress function called wp_enqueue_script() to add the scripts. A very important note is that you need wp_head() to be present in your theme or this will not work.

The parameters for wp_enqueue_script() are simple. The name you want to give to the script, it’s path, it’s dependencies if any, and it’s version number.

Next we need to include the stylesheets needed to style the Nivo slider. We are going to do that in a similar manor except using wp_enqueue_style().

As you can see they are very, very similar. It would probably be best to change the function names to something prefixed with an acronym of your theme’s name. This helps stop conflicts. Just remember to change any reference to the function names. So if your theme is called ‘My Super Awesome Theme’ you could use ‘MSAT’, so theme_add_styles() might become msat_add_styles(). Simplez.

Creating Nivo Slider’s HTML Markup

The WordPress Featured Image Meta Box...Now we have included the scripts & stylesheets needed we can finally get this thing working. The HTML markup if fairly simple, but because we are in WordPress it has to be generated by WordPress. That makes it slightly difficult & while I have played around with a few different methods, I am going to show you the one which seems to use the least resources and makes it the easiest for the user.

The method involves creating a category called ‘featured’, but could be named anything you wish, and then using the post thumbnails system to create thumbnails to fit the size you want Nivo slider to be. To do that you will need to enable custom thumbnails in your functions.php file by using the following code:

Please remember you may need to change the width (960) and height (320). These are just the sizes I used when testing. Now when creating a post you will notice that you have a new box on the right hand side under ‘Post Tags’. Just upload the image you want to use on the slider & press the ‘use as featured image’ link. Oh and remember to tick the ‘featured’ category or it won’t show up on the slider. Now all you need to do is create the loop in your index page.

So open up index.php, if that’s where you want the slider, find the location you have chosen for your slider and drop in this code:

Now we have the code to produce the images in the correct HTML markup we need to initialize Nivo slider.

Initializing Nivo Slider

Okay, we are on the final straight now. Let’s get the slider working. All you need to do is either create a .js file and put the following code in & then add a wp_enqueue_script() to the function we created before, or you can just paste it in the head under wp_head() inside script tags. It is your choice.

You can change the settings as you like. For a full list of parameters go to the Nivo slider website.

All Done!

That’s it. Hopefully if you load up your home page you will have a fully working Nivo Slider. There are a lot of steps granted, but it’s easy once you get into the swing of it.

Oh, you may be wondering why I’ve used jQuery instead of the $ well that’s because if you load jQuery using the enqueue script method it is loaded in no conflict mode by WordPress so the dollar sign reference doesn’t exist.

As always if you have any questions about the implementation of Nivo Slider I have given here, feel free to ask. If you have any questions about the Nivo slider script then I probably can’t answer them as I did not write it. You should instead direct those questions to Dev7Studios, who created the Nivo slider script.

275 Comments

Author’s gravatar

Hello,

Thanks for this great tutorial. I’m having a problem though. Which name should I give to the .js file and where should I place it?

Reply
Author’s gravatar author

Hi marq,

You can name the file anything you like and place it anywhere within your theme folder. As long as you update the paths in the code to match where you choose to place them it should still work without any problems.

Hope that helps.

Author’s gravatar

Thank you for this tutorial. For me, the plugin is working, but there are a couple of problems.
First, the query, which I copied from the post and not modified, is returning only the url of the same page on which the slider is inserted. A category named feature exists and it has exactly 5 posts. Why wouldn’t the query retrieve these posts?
Second, custom css is not provided anymore and the default nivo style is really pure. For example prev and next buttons are simple text. Are there free templates that can be downloaded?

Reply
Author’s gravatar author

Hi Alberto,

I’m not sure why the query would do that. You could try running wp_reset_query() just before the query so that WordPress is starting from a standard query. Also make sure that the slug of the category matches the name used as it can be changed automatically for various reasons.

The download of Nivo available at the official website does come with a default theme, but you must remember to add the theme-default class to your sliders container to use the styling. Take a peek at the demo file that comes inside the zip to see how to do it.

Author’s gravatar

Old post, But still relevant and good help. However I cannot get rid of the error – ‘jQuery is not defined’, I know its must likely silly but grrr!

Reply
Author’s gravatar

Ah, never mind I fixed it. It didn’t seem to like the way I was bringing jQuery in. Thanks!

Author’s gravatar

Hi Paul,

Thanks for the tutorial I have this working on my wamp server for testing at the moment, but all seems good as far as your tutorial goes on WordPress 3.4.1.

I am after some advice though as would like to activate one of the Nivoslider themes but unsure how to include this code? I have linked the theme.css file ok, and added the <div with "slider-wrapper theme-dark", but I am getting some mixed results.

Thanks,

Ant

Reply
Author’s gravatar author

Hi Anthony,

I’m afraid I haven’t really used the themes much, but as far as I know you just add the class, as you have, into the sliders wrapper. It always seemed to work in the few times I have used them.

I guess that isn’t much help, sorry. 🙁

Author’s gravatar

Thanks a lot. This bit saved me! I read on the dev7studios.com website that I should use the $. I could not get the slider to work until I changed this.

[quote]Oh, you may be wondering why I’ve used jQuery instead of the $ well that’s because if you load jQuery using the enqueue script method it is loaded in no conflict mode by WordPress so the dollar sign reference doesn’t exist.[/quote]

Reply
Author’s gravatar

Hi Paul
I posted here about 18 months ago talking of the trouble I had with NivoSlider and NS in WordPress. Well I had part success back then but I am dealing with it again & today I had problems putting it on a site I am converting to a WP theme. I tried x3 NS WP plugins and hard coding none with full success. The site is on my online test site at:http://onlinewpthemetest.richard-dickinson.com/
I would be grateful if you would take a look & help if possible. Most of the pages are custom page templates and are static-only the blog page displays posts but I want the NSlider on all pages. Thanks & best wishes

Reply
Author’s gravatar author

Hi Richard,

A big, big tip for working with CSS & JS is to learn how to use a debugging console. IE 9/10 has one (hit F12), Chrome has one (again F12) and Firefox has an extension called Firebug, they are all free and totally indispensable in web development. I’ve posted the list of errors I get when viewing your site in a pastebin for you:

http://pastebin.com/wwVCkJJV

If you can fix all the 404s your slider will probably start working. If not give me another buzz, either by comment or email & I’ll see if I can help you further.

Author’s gravatar

Hi Paul
Thanks for taking a look at my site.I managed to get the slider working both on the custom page template pages and the blog posts page. I think it wasn’t displaying due to a JQuery conflict.

I use firebug routinely. I am just converting this site from static html to a WordPress theme and have plenty of other issues to fix with it (I also converted it to HTML5). Thanks for your report &
highlighting 404 errors & kind offer of further help but I hope I can sort these!

It was interesting for me revisiting NivoSlider both on a WordPress theme & otherwise.Your article was a great help again! I must also look at your next post on styling custom controls for it…
Many thanks and best wishes

Author’s gravatar

hi,
Article was updated to the latest version of nivoslider?
thank you

Reply
Author’s gravatar

Hi Paul,

I have tried numerous ways of integrating nivo slider into wordpress theme but somehow I have been failing. Initially, I was not even able to get it working, but now I have managed to display the layout. Some where I think I am doing it wrong because the image is not appearing. Can you please take a look at the link?

Thanks,
Justin.

Reply
Author’s gravatar author

Hi Justin,

Well, in the infamous words of the Mythbusters: “Well there’s your problem!”

If you look at the source code for your site you have jQuery & Nivo Slider attached twice. Once at the top & again at the bottom. You’ll need to find how the bottom links are being attached (I’m guessing they are hardcoded into the footer?) and remove them. That should fix your problem.

Author’s gravatar

Hi Paul,
Thanks for the reply.

I have this code in the footer after

<script src="/js/jquery.nivo.slider.js” type=”text/javascript”>

$(window).load(function() {
$(‘#slider’).nivoSlider();
});

The problem is if I remove any line the whole slider crashes.
Can you please point out where I am going wrong?

Thanks,
Justin.

Reply
Author’s gravatar

Sorry missed a part of the code above.

Reply
Author’s gravatar author

Hi Justin,

You only need the last script block to initialize Nivo as you already have jQuery & Nivo attached in your header. Nivo now requires the jQuery .on() method to work and it was not added to jQuery until version 1.7. You are overwriting the version of jQuery you have in the header with the version of jQuery you have in your footer which is 1.4.2. Hopefully if you fix that everything should start working.

Author’s gravatar

Wow!! Thanks a lot!! I couldn’t figure how to use the .on() method but I changed the version of jquery in the footer and viola …. its working!! Tested successfully in my localhost!

I will look into the .on() method and try to figure it out or else I will get back to you. Thanks once again.
Justin.

Reply
Author’s gravatar

Hi Paul,

I am now having a new problem…the slideshow is starting normally but if we click on the navigation arrows a couple of times the image is expanding above the menu bar and also the slide title is positioned in the middle of the image. Can you please point out the problem? Is it just a css defect or am I missing something in the code?

Reply
Author’s gravatar author

Hi Justin,

I’m not too sure, but best guess would be that you still have two conflicting versions of jQuery & Nivo slider attached to your page. You have jQuery & Nivo attached in your header, and then again in your footer. You can only have one of each on your page or it can cause some very strange problems to occur.

Author’s gravatar

Hi!

I do it all, but do not show nothing! Need to do any more thing else? I make my own theme from zero.

Thanks a lot!

Reply
Author’s gravatar author

Hi,

Unfortunately without more detail it could be anything. Blank-ness, or a white screen of death as it is sometimes called, is a developers nightmare as there is a problem but it may not always be obvious where. My only advice is to check everything over again to make sure there are no mistakes & possibly use a debugger such as Chrome developer tools to make sure there are not missing files or console errors.

Author’s gravatar

Hello Paul,

Just want to say a big thanks, i was searching for the loop 😉
Nivo Slider V3.2 works just fine.

Thanks again for sharing.

Reply
Author’s gravatar

Thank you Paul you really made my day! I wonder how to modify the loop to get post title and excerpt as well as post thumbnails??

Reply
Author’s gravatar

Hi Paul ,
can I ask for more advanced tutorial. I’m using twentytwelve as parent theme and I want to make slider to get 140px*140px thumbnail (based on category slug ) to the left and post title plus post excerpt to the right. I know I’m asking to much but I’m stuck in a project any help will be appreciated !

Reply
Author’s gravatar author

Hi,

There is another tutorial I did using Nivo Slider that shows how to add in the post title & excerpt as a caption, but I’m not sure if that is what you are shooting for.

If it’s not drop me an email or leave a comment on that article & I’ll be happy to see if I can help you more.

Author’s gravatar

Hi Paul,
Thank you very much for your response. I saw the tutorial you referred to but I need some thing different. I’m building my theme using twenty twelve theme as a parent theme. I made some tweaks to the content.php file to show excerpt and 140*140 px thumbnail based on category slug here is the code to make it clear for you :
http://pastebin.com/BNb3qGJC
I want to make a slider in index page to get the post thumbnail by category slug and get 5 posts excerpt by category I want a loop to get this job done and a way to wrap it in slider and get it to work. I hope made myself clear ( English is not my first language )
thank you in advance!

Author’s gravatar

Hello thank you for the tutorial.. I have a question, I would like to make the featured images separate from a blog post, is this possible using the above method?

I find that the only way an image will show up in the slider is if it’s a featured post, but then the image will show up in both the blog posts and in the slider. How do I only have it show up in the slider?

Hope you can advise, thank you!

Reply
Author’s gravatar author

Hi Grace,

To do that you create your slider as normal but create an array before the loop starts and record the post IDs as they are output for the slider. Then you pass these to your normal query under the exclude parameter. That will remove them from the normal loop so they aren’t duplicated. So a quick dirty example is:

I’ve cut the loops out and replaced with comments for brevity, but I’m hoping it gets the idea across. If you do need further help please feel free to send me an email via the contact form & I’ll try to help you as much as I can.

Author’s gravatar

Any Idea how to get the slider to work when loaded with ajax?

Reply
Author’s gravatar author

Hi John,

It depends on how you mean. If you are asking how to get Nivo to start working when it has been loaded via AJAX or after page load then that depends on your project. You need to call the Nivo slider initialization code after the slider has been added to the current page (DOM).

In the case of a Bootstrap modal, that would be using the show.bs.modal event. If you can put an example of how your code is being loaded in a pastebin I might be able to help a little further. If you don’t want to make the code public feel free to email me instead.

Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more