It’s an interesting task.
We can make a custom Slider Menu in Dashboard then featured images can be upload from there so that those images can be called dynamically in slider.
First we have to make Theme Support and register Post Type in functions.php
add_theme_support( 'post-thumbnails', array( 'post', 'slider' ) );
add_image_size( 'slider-image', 1024, 550, true );
function create_post_type() {
register_post_type( 'slider',
array(
'labels' => array(
'name' => __( 'Slides' ),
'singular_name' => __( 'Slide' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Slide' ),
'edit_item' => __( 'Edit Slide' ),
'new_item' => __( 'New Slide' ),
'view_item' => __( 'View Slide' ),
'not_found' => __( 'Sorry, we couldn\'t find the Slide you are looking for.' )
),
'public' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'menu_position' => 14,
'has_archive' => false,
'hierarchical' => false,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'Slide' ),
'supports' => array( 'title', 'thumbnail' )
)
);
}
add_action( 'init', 'create_post_type' );
Now, in the file where the slider images run, we need to make a loop and inside the loop demonstrate query to get the images from database as well as call the query result.
<?php
if(!is_paged())
{
$args = array('post_type' => 'slider', 'posts_per_page' => 4);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$image = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'slider-image' );
?>
<div data-src="https://wordpress.stackexchange.com/questions/162686/<?php echo $image; ?>"> </div>
<?php }
}
}
?>
That’s it you’re done! Now images uploaded from your Slides menu will be shown in slider.