CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   WordPress   How to Create Custom Post Type Slider in WordPress Without P ...

How to Create Custom Post Type Slider in WordPress Without Plugin ?

August 26, 2018 by SNK 10

How to Create Custom Post Type Slider in WordPress Without Plugin

In this tutorial we will learn how to create custom post type slider in WordPress. If you are thinking about to create slider WordPress without plugin then, this is the tutorial for you.

We are going to create custom post type named “slider” and then use  it to store caption and image and finally fetch it at the desired location in the front end. Mostly below the navigation bar in our website.

Let’s get started

What we are going to do ?

  1. Create custom post type named “Slider”.
  2. Remove slug and WP Editor field from our custom post type.
  3. Add Featured Image Support if using a custom built theme.
  4. Add Custom Meta box addon to save link (if necessary).
  5. Fetch the custom post type (slider) content in the front end.

Step 1 :- Create Custom Post Type Called Slider

Let’s break down on what we are going to need inside a slider.

  • Title.
  • Caption.
  • Link

By default WordPress gives the ability to add title, description, excerpt, slugs into custom post type. We are not going to need slug and editor field since we are using title,excerpt and featured image field to store our information. Lets create custom post type first.

Create Custom Post Type Called Slider
PHP
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
/**
* Create Custo Post Type for Slideres
*/
 
function create_slider_post_type() {
 
$labels = array(
'name' => __( 'Sliders' ),
'singular_name' => __( 'Sliders' ),
'all_items'           => __( 'All Sliders' ),
'view_item'           => __( 'View Slider' ),
'add_new_item'        => __( 'Add New Slider' ),
'add_new'             => __( 'Add New Slider' ),
'edit_item'           => __( 'Edit Slider' ),
'update_item'         => __( 'Update Slider' ),
'search_items'        => __( 'Search Slider' ),
'search_items' => __('Sliders')
);
 
$args = array(
'labels' => $labels,
'description' => 'Add New Slider contents',
'menu_position' => 27,
'public' => true,
'has_archive' => true,
'map_meta_cap' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array('slug' => false),
'menu_icon'=>'dashicons-format-image',
'supports' => array(
'title',
'thumbnail','excerpt'
),
);
register_post_type( 'slider', $args);
 
}
add_action( 'init', 'create_slider_post_type' );

 

Step 2 :- Remove Slug support from custom post type

We are not going to make the post feature of slider visible is the front end except in homepage so, we are removing it.  As for editor, we can use it to store caption and excerpt for the link. But i would like to make things cleaner and more maintainable.

So, we are going to make custom fields for the link on the next step. You can use the editor by simply comment out the remove_post_type_support( 'slider', 'editor' ); below.

Remove Slug from Slider Post Type
PHP
1
2
3
4
add_action( 'init', function() {
    remove_post_type_support( 'slider', 'editor' );
    remove_post_type_support( 'slider', 'slug' );
} );

Step 3 : Add featured Image Support

This step may not be required for you, as most of the theme that you buy from the store automatically comes in featured image support enabled.

If you are using custom theme then, you need to add some piece of code inside the functions.php file in WordPress. Else it will not show the featured image option inside your admin panel while creating a slider post.

1
2
3
4
5
6
7
function cih_theme_support(){
 
   add_theme_support( 'post-thumbnails' );
   add_image_size( 'slider_image','1024','720',true);
 
}
add_action('after_setup_theme','cih_theme_support');

 

Step 4 : Add Link Field Creating Custom Meta-Box Field in WordPress Admin

Note :  You can skip this step, if you want to use editor as the caption and excerpt as link holder.

Lets create new custom field for link field inside a slider post type.

PHP
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
function sliderLink_add_meta_box() {
   add_meta_box('slider_link','Slider Link','slider_link_callback','slider');
}
 
function slider_link_callback( $post ) {
 
   wp_nonce_field('slider_link_save','slider_link_meta_box_nonce');
   $value = get_post_meta($post->ID,'_slider_link_value_key',true);
   ?>
    <input type="text" name="slider_link_field" id="slider_link_field" value="<?php echo esc_attr( $value ); ?>" required="required" size="100" />
   <?php
}
add_action('add_meta_boxes','sliderLink_add_meta_box');
 
 
function slider_link_save( $post_id ) {
   if( ! isset($_POST['slider_link_meta_box_nonce'])) {
      return;
   }
   if( ! wp_verify_nonce( $_POST['slider_link_meta_box_nonce'], 'slider_link_save') ) {
      return;
   }
   if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
      return;
   }
   if( ! current_user_can('edit_post', $post_id)) {
      return;
   }
   if( ! isset($_POST['slider_link_field'])) {
      return;
   }
   $slider_link = sanitize_text_field($_POST['slider_link_field']);
   update_post_meta( $post_id,'_slider_link_value_key', $slider_link );
}
add_action('save_post','slider_link_save');

If you have gone properly through this tutorial then, new post type slider would appear just below the comments section in WordPress admin dashboard.

Custom Slider Post Type Below Comments Section in WordPress

If you click on it., default with removed WP-Editor and custom field will show up. Go head enter your details and save it. Upload your image from the featured image section.

Removed Editor and Slug Admin Dashboard

 

Step 5 : Fetching Custom Post Type Data Into the Front End

Now, its time to fetch the data of custom post type into the front end. I will use the sample slider that i got from bootsnipp and integrate in my front end. If you have already your slider prepared. Then you need to make the changes accordingly. Take a look at below code very carefully.

Fetching Data From the Frontend
PHP
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
47
48
49
50
51
52
<div class="container">
   <div class="row">
      <?php
      $arg = array(
         'post_type'         => 'slider',
         'posts_per_page'    => 5,
         'order'             => 'ASC'
      );
      $slider = new WP_Query($arg);
      $j = 0;
      $post_count = wp_count_posts('slider')->publish;
      ?>
      <!-- Carousel -->
      <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
         <!-- Indicators -->
 
 
         <ol class="carousel-indicators">
                <?php for($i=0;$i<$post_count; $i++): ?>
            <li data-target="#carousel-example-generic" data-slide-to="<?php echo $i; ?>" class="active"></li>
                <?php endfor; ?>
         </ol>
         <!-- Wrapper for slides -->
         <div class="carousel-inner">
                <?php while($slider->have_posts()) : $slider->the_post(); ?>
            <div class="item<?php echo $j==0 ? ' active': '';?>">
               <?php if(has_post_thumbnail()): the_post_thumbnail('slider'); endif; ?>
               <!-- Static Header -->
               <div class="header-text hidden-xs">
                  <div class="col-md-12 text-center">
                     <h2>
                        <span>Welcome to <strong><?php the_title() ?></strong></span>
                     </h2>
                     <br>
                     <h3>
                                <a href="<?php echo get_post_meta(get_the_ID(),'_slider_link_value_key', true); ?>"><span><?php the_excerpt(); ?></span></a>
                     </h3>
                  </div>
               </div><!-- /header-text -->
            </div>
                <?php $j++; endWhile; wp_reset_query(); ?>
         </div>
         <!-- Controls -->
         <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left"></span>
         </a>
         <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right"></span>
         </a>
      </div><!-- /carousel -->
   </div>
</div>

Remember that i am using bootstrap as a front end framework. You need to integrate it too if you are copying slider code that have been used above

Finally the CSS :-

Final CSS
CSS
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
.container {
margin-top: 20px;
}
 
/* Carousel Styles */
.carousel-indicators .active {
background-color: #2980b9;
}
 
.carousel-inner img {
width: 100%;
max-height: 460px
}
 
.carousel-control {
width: 0;
}
 
.carousel-control.left,
.carousel-control.right {
opacity: 1;
filter: alpha(opacity=100);
background-image: none;
background-repeat: no-repeat;
text-shadow: none;
}
 
.carousel-control.left span {
padding: 15px;
}
 
.carousel-control.right span {
padding: 15px;
}
 
.carousel-control .glyphicon-chevron-left,
.carousel-control .glyphicon-chevron-right,
.carousel-control .icon-prev,
.carousel-control .icon-next {
position: absolute;
top: 45%;
z-index: 5;
display: inline-block;
}
 
.carousel-control .glyphicon-chevron-left,
.carousel-control .icon-prev {
left: 0;
}
 
.carousel-control .glyphicon-chevron-right,
.carousel-control .icon-next {
right: 0;
}
 
.carousel-control.left span,
.carousel-control.right span {
background-color: #000;
}
 
.carousel-control.left span:hover,
.carousel-control.right span:hover {
opacity: .7;
filter: alpha(opacity=70);
}
 
/* Carousel Header Styles */
.header-text {
position: absolute;
top: 20%;
left: 1.8%;
right: auto;
width: 96.66666666666666%;
color: #fff;
}
 
.header-text h2 {
font-size: 40px;
}
 
.header-text h2 span {
background-color: #2980b9;
padding: 10px;
}
 
.header-text h3 span {
background-color: #000;
padding: 15px;
}
 
.btn-min-block {
min-width: 170px;
line-height: 26px;
}
 
.btn-theme {
color: #fff;
background-color: transparent;
border: 2px solid #fff;
margin-right: 15px;
}
 
.btn-theme:hover {
color: #000;
background-color: #fff;
border-color: #fff;
}

This is it for the tutorial creating custom slider in WordPress. If you have another problem integrating slider in WordPress, let me know via comments or contact page.

SHARE ON
Buffer

Enjoyed this article?

Like us on

WordPress how to add slider in wordpress homepage how to add slider in wordpress page how to create custom slider in wordpress how to create slider in wordpress without plugin how to create slider plugin in wordpress step by step how to edit slider content in wordpress

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

10 Responses...

  1. Avatar for SNKLipe Shtogu says

    April 3, 2020 at 8:23 am

    Hi
    Great Tutorial
    Please can you tell me how to add another field on slider for adding small images (logos of products) displayed on slider
    Thank you

    Reply
    • Avatar for SNKShashank Bhattarai says

      September 16, 2020 at 1:08 am

      Yes, we can but i first want to know where do you want to display those logos ? Already there will be image that you want to slide. Will the image will be placed overlapping the current featured image?

      Reply
  2. Avatar for SNKlamin says

    May 11, 2020 at 6:23 am

    how am i going to add this to my wordpress

    Reply
    • Avatar for SNKShashank Bhattarai says

      September 16, 2020 at 1:06 am

      Write all the codes above the page inside function.php. In sidebar you will see a custom post type where you can add title with featured image. It will act as slider image. Later you can use for loop as mentioned in the post inside your slider for looping.

      Reply
  3. Avatar for SNKJames Whalen says

    September 13, 2020 at 5:34 pm

    Did you post a link where I can see the finished slider (I could not find it.) Maybe you forgot?

    Reply
    • Avatar for SNKShashank Bhattarai says

      September 16, 2020 at 1:11 am

      No Since, it is not a HTML slider and WordPress slider to create the demo for it would need to create a fresh WordPress site which is not possible to show only for demo purposes. However after all the steps you followed and added new sliders you can copy the (Step 5 : Fetching Custom Post Type Data Into the Front End) anywhere in the page like header footer or single.php wherever you want to display your slider.

      Reply
  4. Avatar for SNKIndrajeet Pawar says

    March 15, 2021 at 8:37 am

    Hello Sir,Thanks for the detailed tutorial as I am beginner to wordpress, I dont know where to add HTML and CSS of slider.

    Reply
    • Avatar for SNKSNK says

      March 27, 2021 at 10:35 am

      You can codes where ever you want in your template file. In general the slider would be in your homepage which is index.php. If you want to in single post like the blog post it should be added inside single.php else if its separate page then, it should be inside your page template.

      Reply
  5. Avatar for SNKLucas Gomes says

    May 11, 2021 at 4:21 pm

    I created everything just right, and it’s working, but when I click on the next slide, it does nothing, even if I have more than 1 slide created, and if I leave the ‘posts_per_page’ => 5, it lists all the slides one below the other. How can I proceed?

    Reply
    • Avatar for SNKSNK says

      June 8, 2021 at 2:52 am

      Can you inspect element and check if the element is present in Dom or not. Reasons could be either the all data have not been fetched from the server or there is some issue with bootstrap slider.

      The two things you can check is carousel functionality and number of records is greater than one or not.

      Reply

Leave a Reply Cancel reply

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

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.