I’m using WordPress. For client testimonials, I have created Custom Post Type which is working fine. After that, I’m creating a shortcode to display post data at the frontend.
Problem – I get all the post data, but I’m not getting a slider.
Output I get – current result
- I have included slick js and CSS in function.php properly.
function ec_theme_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css' );
wp_enqueue_style( 'custom-css', get_template_directory_uri() . '/css/style.css' );
wp_enqueue_style( 'slick-css', get_template_directory_uri() . '/css/slick.css' );
wp_enqueue_style( 'fontawesome-css', 'https://pro.fontawesome.com/releases/v5.15.4/css/all.css' );
wp_enqueue_script('bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true);
wp_enqueue_script('jquery', get_template_directory_uri() . '/js/jquery-3.6.0.min.js', array(), true);
wp_enqueue_script('slick', get_template_directory_uri() . '/js/slick.min.js', array(), true);
}
add_action( 'wp_enqueue_scripts', 'ec_theme_scripts' );
- Here is the WordPress loop code snippet, where I’m fetching slider data from custom post type.
function create_testimonials_shortcode() {
$args = array(
'post_type' => 'Testimonials',
'publish_status' => 'published',
);
register_post_type('Testimonials', $args);
?>
<?php
$query = new WP_Query($args);
if( $query -> have_posts() ) :
?>
<div class="testimonial_slider">
<?php while( $query -> have_posts() ):
$query -> the_post() ;
?>
<div class="testimonial-slide">
<div class="testimonial-content">
<p> <?php the_content(); ?> </p>
</div>
<div class="image-title-wrapper">
<?php $img_url = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<img src = "<?php echo $img_url; ?>" >
<h4 class="testimonial-name"> <?php the_title();?> </h4>
</div>
</div>
<?php endwhile; ?>
</div>
<?php
endif;
wp_reset_postdata();
?>
<?php
}
add_shortcode( 'testimonials', 'create_testimonials_shortcode' );
- At last, Here is the jquery for the slick slider.
$(document).ready(function() {
$('.testimonial_slider').slick({
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
dots: true,
autoplay: true,
autoplaySpeed: 5000
});
});
I have checked everything but didn’t get what has gone wrong exactly.