WordPress Featured Posts using Nivo Slider
Today we will look in to how to create a wordpress featured posts slider using Nivo Slider. If you have a wordpress based blog or website, you may wish to highlight a set of interesting posts of your blog or website on your homepage. You may have already searched for a wordpress plugin to do that. But this can be done without using a plugin.

First, we will look in to creating a input to select the wordpress posts that to be included in to the Featured posts slider.
All the below codes should go inside your theme’s functions.php file.
Create option to select posts for Featured Slider
- add_action("admin_init", "selection_meta_box");
-
- function selection_meta_box(){
- add_meta_box("featured-post", "Set Featured", "featured_post", "post", "side", "low");
- }
-
- function featured_post(){
- global $post;
- $meta_data = get_post_custom($post->ID);
- $featured_post = $meta_data["_featured_post"][0];
- $selected = ($meta_data["_featured_post"][0] == "yes") ? 'checked' : '';
- echo "<p>";
- echo "<input $selected type='checkbox' name='featured_post' value='yes' />";
- echo "<label>Select this post as Featured.</label>";
- echo "</p>";
- }
This will create a meta box inside the post editor with a checkbox option to choose which posts need to be included in to Featured posts slider.
What if you want this option also for your Custom Post types? Simply change the post type parameter in the add_meta_box function.
- //change post-type to your custom post type
- add_meta_box("featured-post", "Set Featured", "featured_post", "post-type", "side", "low");
Or if you want to enable this option to all your post types,
- $post_types_array = get_post_types();
- foreach ( $post_types_array as $post_type ) {
- add_meta_box("featured-post", "Set Featured", "featured_post", $post_type, "side", "low");
- }
Now we need to save the selected posts in to the database.
Adding post thumbnail support to the theme
- add_theme_support( 'post-thumbnails' );
- add_image_size( 'sliding-images', 800, 400, true );
- }
The function add_image_size() will create a separate custom sized image for our slider.
Note: You need to set a featured image using the “Featured image” feature by wordpress. Otherwise, the slider will not work.
This will allow us to select which image from the post gallery to display on the slider.
Function to query Featured posts
- function show_featured_posts($numbers) {
- global $post;
- //get $numbers of featured posts
- $featured_posts_array = get_posts( 'post_type=any&meta_key=_featured_post&meta_value=yes&numberposts=$numbers&post_status=publish');
- }
From the above query, we are getting the latest featured posts in post date order. Let we add the image loop in the same function.
- function show_featured_posts($numbers) {
- global $post;
- //get $numbers of featured posts
- $featured_posts_array = get_posts( 'meta_key=_featured_post&meta_value=yes&numberposts=$numbers&post_status=publish');
- $output .= '<div class="slider-wrapper theme-default">';
- $output .= '<div class="ribbon"></div>';
- $output .= '<div id="slider" class="nivoSlider">';
- foreach ($featured_posts_array as $post) : setup_postdata($post);
- $nivo_title = "#nivo".get_the_ID(); //assign the postID as title of the image
- $caption .= "<div id='nivo".get_the_ID()."' class='nivo-html-caption'>
- <h2><a href='".get_permalink()."'>".get_the_title()."</a></h2>
- ".get_the_excerpt()."
- </div>";
- endforeach;
- $output .= '</div>';
- $output .= $caption;
- $output .= '</div>';
- return $output;
-
- //reset WP query
- wp_reset_query();
- }
So now we got the function to output the selected Featured posts. This function will return the selected featured posts and with this you can integrate any slider of your choice by changing the class name or ID of the div tags.
Next step of our tutorial is importing the necessary nivo files in to the wordpress theme folder.
Put Nivo Slider files in to theme folder
Download latest Nivo Slider version from http://nivo.dev7studios.com/
Copy and paste the ‘themes’ folder from the extract in to your wordpress theme folder yourdomain/wp-content/themes/theme-name/. Copy and paste jquery.nivo.slider.pack.js and nivo-slider.css to your ‘js’ or ‘lib’ folder. You need to be aware of where you are placing the files because you need to set the correct path to these files in the later part.
Include Nivo slider files to the theme
- function include_my_scripts() {
- wp_deregister_script( 'jquery' );
- wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
- wp_register_script( 'nivopack', get_bloginfo('template_directory').'/js/jquery.nivo.slider.pack.js');
- wp_enqueue_script( 'jquery' );
- wp_enqueue_script( 'nivopack' );
- }
-
- add_action('wp_enqueue_scripts', 'include_my_scripts');
-
- add_action('wp_print_styles', 'add_my_stylesheets');
- function add_my_stylesheets() {
- wp_register_style('nivo_theme_style', get_bloginfo('template_directory').'/themes/default/default.css');
- wp_register_style('nivo_main_style', get_bloginfo('template_directory').'/js/nivo-slider.css');
- wp_enqueue_style( 'nivo_theme_style');
- wp_enqueue_style( 'nivo_main_style');
- }
-
- //Call the Nivo function in to the footer
- add_action('wp_footer', 'nivo_functioncall');
- function nivo_functioncall() {
- echo '<script type="text/javascript">
- $(window).load(function() {
- $("#slider").nivoSlider();
- });
- </script>';
- }
The new version of Nivo slider offers 3 set of slider themes ‘default’, ‘orman’ and ‘pascal’. You can change between these themes by changing the theme css file
- wp_register_style('nivo_theme_style', get_bloginfo('template_directory').'/themes/default/default.css');
Place the slider on desired location
Final thing, call the output function show_featured_posts() from your desired location. For example, if you have the index.php as your front page, then paste the following code after the line get_header() in your index.php file.
Why not implement a shortcode?
Now you can put the shortcode [featured numbers="5"] anywhere in your posts or pages to display the Featured posts Slider, change the numbers value to any number you want.
I hope this tutorial will help you for a good start of creating a WordPress Featured Slider.
If you want this slider with more options and as a wordpress plugin, check the premium plugin by Dev7studios Nivo












Detailed tutorial, thanks for sharing it.
Hi, great tutorial, but i have some errors:
Notice: Undefined variable: output in wp-content/themes/xxxxxxxxx/functions.php on line 71
Notice: Undefined variable: caption in wp-content/themes/xxxxxxxxx/functions.php on line 81
is it ok, how can I fix it?
Thanks
You don’t need to worry about it. Its just an error notice and not an actual error. Once you disable the error_reporting(E_ALL) in your pnp.ini file, this error will go off.
It’s actually wp debug mode, I’m just checking new code for errors before publish it, but have not enough knowledge in it. Great tutorial, I will use it in new site created with blank theme.
Thanks a lot!
one more question, how can I use this with my pages? not only posts
if you want to show only posts, simply use the shortcode in your page(s). If you want to include pages along with posts in your featured slider, add below code in to selection_meta_box function
add_meta_box(“featured-post”, “Set Featured”, “featured_post”, “page”, “side”, “low”);
thanks for answer, trying to it use along with posts, I can add page thumbnail and also can set page as featured but it doesn’t show in slider
Sorry, i forgot to include post type in the get_posts query. Add post_type=any to the get_posts function to get all post types.
$featured_posts_array = get_posts( 'post_type =any & meta_key=_featured_post & meta_value=yes & numberposts=$numbers & post_status=publish');i will update the post soon as possible. Thanks for the heads up!
Thanks a lot, it works!
This is really fantastic! Shanks a lot for sharing this. It will definitely come in handy in many future projects.
One thing though and this might be important for others as well. How difficult would it be to add the permalink to the image as well? Every attempt I have made so far results in white screen of death, Your help would be much appreciated.
Thanks a bunch!
Oops meant thanks, not shanks. Also, it would also be helpful to know how to add a proper variable for an alt tag within the image. For instance something like:
“alt” => “$alt_variable”
Not sure what to use with that. If I find out in the meantime I will be happy to share. I think the alt tag is required for valid xhtml compliance.
This worked. I hope it benefits you.
$output .= '<a href='.get_permalink().' rel="nofollow">';
$output .= get_the_post_thumbnail(get_the_ID(), array(1000,300), array( "class" => "post_thumbnail", 'title' => $nivo_title )); }
$output .= '</a>'
Still looking for an alt variable. Perhaps a custom metabox might be a good solution. I will share my findings.
For some reason I cannot post my code here but here it is on pastebin. If the author of this blog could please clean up this mess, it is appreciated.
http://pastebin.com/KALbALwk
Hi Brentini,
You did it right. Here i added how to include alt tag to the image.
$output .= '<a href='.get_permalink().' rel="nofollow">';
$output .= get_the_post_thumbnail(get_the_ID(), array(800,400), array( "class" => "post_thumbnail", 'title' => $nivo_title, 'alt' => get_the_title() ));
$output .= '</a>';
Kannan,
You know what, I think that works absolutely perfectly for what I need. I was trying to do something else, but this is actually better anyway, now that I think about it. Thanks a bunch! I hope this helps out other readers here too for their projects.
Best regards to you and good luck with your website!
Hi guys. I’m pretty new to all this stuff. I’m just wondering if it’ll work for Joomla? Please advise. Thanks
Sorry but this cannot be used in joomla. This tutorial is only for wordpress.
Thanks for an amazing tutorial/slider. I have used 20+ different slider and this one is the only one that works precisely as a slider should!
Is it possible to use categories instead of “Feature this article”.
Then it would be much easier to see which posts are in the slider, from the backend.
Already shared this tutorial with my wp network
Hi Martin, Yes it is possible. Simply you can add category parameter in to the get_posts function. If you don’t need as a select option method, remove the meta key parameters.
$featured_posts_array = get_posts( 'category=15 & numberposts=$numbers & post_status=publish');
For more about get_posts function check this out http://codex.wordpress.org/Template_Tags/get_posts
Thanks!
Worked like a charm!!
I inserted the code into function.php and the nivo slider to the theme folder. Now I get this error: Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /home/www/web266/html/pginit/wp-content/themes/WhosWho/functions.php:56) in /home/www/web266/html/pginit/wp-content/plugins/si-contact-form/si-contact-form.php on line 1931
Do you have any idea what this could be?
Thanks in advance!
Nice bro, i implement it.
I am SO close to getting this to work. I have hacked it apart and it is now working as I want it, minus the fact that it shows ONLY 1 post on the front page now. The ones that are featured are gone. Which is fine.
Any ideas?
I should have said, the posts are duplicated, so it’s one post over and over again.
did anyone figure out this problem yet?
Just what I was looking for, as per previous comments I adapted it so I can use it for pages too. Also using it to display a list of featured pages/posts which aren’t in the nivo slider.
Good work!
HI
I have tried this option, however, I am having this trouble:
$post_types_array = get_post_types();
foreach ( $post_types_array as $post_type )
{
add_meta_box(“featured-post”, “Set Featured”, “featured_post”, “side”, “low”);
}
I get an error message:
Fatal error: Call to undefined function add_meta_box() in C:\xampp\htdocs\wordpress\wp-content\themes\EverBluee\functions.php on line 602
Can you please help
I’ve been trying to modify this code so that I can declare a category for the posts the slider will display, but I’m having a hard time doing so. For the end result, I would like to be able to write
into my template page with the category of the posts I want to display on this template page being “Newspaper.” Can this be easily accomplished?
if(function_exists(‘show_featured_posts’)) echo show_featured_posts(’5′);
This is the code that didn’t print above.
Oh my goodness, I just cant get this right: if(function_exists(‘show_featured_posts’)) echo show_featured_posts(’5′, ’6′);
With the ID of the category being 6
I’m even more dumb than i feared, you answered this question for someone else above. Thanks!
Guys this may be a silly question but the problem I am having is:
When I add the the checkbox option to select which posts will appear in the slider, the option is not saved – so if I check the checkbox and then press update to update the post, when the page refreshes the option is unchecked.
any help would be awesome
Sorry, just figured it out
i did every thing with no errors but nothing shown at last!!
could you help me?
First of all, I’d like to say: THANK YOU!!! This has been such a tremendous help and a much more elegant solution to anything I had used in the past.
The only issue I’m having and I believe this is more to do with my custom post type setup than the solution you presented, is that my custom post types do not display when the featured post option is checked.
The code from my functions.php is listed here. Any assistance you could provide would be greatly appreciated. Thank you.
http://pastebin.com/hHkA2bp4
Thanks for posting this!
Is there a way to use the Large Size image rather than a custom size?
excellent tutorial, I hate using the plugin and Love doing things this way. I’m new to PHP and wordpress and this so far is the best tutorial I have put in practice. thank you for your hard work
Hi there,
I tried integrating this, exactly as you put above, and it isnt working… any ideas?
thanks
Hello! I need to add custom shield to the slider and the category name. How can I call they at this part of the functions:
==================================================================
foreach ($featured_posts_array as $post) : setup_postdata($post);
$nivo_title = “#nivo”.get_the_ID(); //assign the postID as title of the image
if ( function_exists(“has_post_thumbnail”) && has_post_thumbnail() ) {
$output .= get_the_post_thumbnail(get_the_ID(), array(620,320), array( “class” => “post_thumbnail”, ‘title’ => $nivo_title )); }
$caption .= ”
“.get_the_title().”
“I WANT CATEGORY HERE”
“I WANT CUSTOM SHIELD HERE”
“;
endforeach;
=================================================================
The custom shield is that:
ID,$key,true);?>
I would like to know too, how to change the size of thumbnail. I tried to change the size in function but dont works, the size are the default always (800,600) I want (620,320)
And have an uncaught TypeError on the js archive “jquery.nivo.slider.pack” the slide is not sliding for next post.
If you can reply fastly, I’ll be very grateful!
The custom shield
ID,$key,true);?>
Hey,
I’m using Custom Content Types plugin, and I have two fields,
before_image and after_image
So, I’m NOT using an actual “Featured Image”, per sae.
I incorporated your functions, but I would like the after_image to be featured.
Here is my full template code (single-project.php)
Before Image: <img src="" />
After Image <img src="" />
Before Image: <img src="” />
After Image <img src="” />
Sorry,
here is my single-project.php template, that display’s two photos using Custom Content Types
http://pastebin.com/XE9DKvLc
ive followed the steps, however the page renders my 2 featured articles images side by side, and below the titles and exerpts….it does not format it into the slider and slide……not sure whats going on, seems like its not linking up to nivo properly
Hi Kannan,
My name is Tim.
First off I would like to say thank you for your diligent work on this Nivo slider option.
I have incorporated your functions.php file into the TwentyTwelve theme at http://store1.timothycaron.com but it is not showing the posts.
I see posts in the source code when I review it, but can’t make them visible.
Am I doing something wrong?
Any help would be greatly appreciated.
Thanks
Tim
Great post! Funny how things just appear… I was looking for a good slider.My template came with an integrated slider too. I didn’t know you can take it down and replace it with another slider!!
it does not format it into the slider and slide thiet ke web dong nai