Home  >  Q&A  >  body text

Why doesn't my Jquery slider display correctly?

I'm trying to implement the Slick Slider library in my WordPress custom theme to display an image slider taken from the Advanced Custom Fields plugin. However, I'm running into an issue where the slider just stacks the images on top of each other and the slider functionality doesn't work.

Here's what I've done so far:

<article>
    <div class="entry-content">
        <h2 class="wp-block-heading has-text-align-center"><?php the_title(); ?></h2>
        <div class="hotel-description"><h2> Hotel Description: </h2> <?php echo $hotel_description; ?></div>
        <div class="hotel-services"><h2> Hotel Services </h2><?php echo $hotel_services; ?></div>

 <div class="hotel-images-slider">
    <?php
    // Output the images
    if ($hotel_image || $hotel_image2 || $hotel_image3) {
        echo '<div>';
        if ($hotel_image) {
            echo '<img src="' . esc_url($hotel_image['sizes']['medium']) . '" alt="' . esc_attr($hotel_image['alt']) . '" class="custom-image-class" />';
        }
        if ($hotel_image2) {
            echo '<img src="' . esc_url($hotel_image2['sizes']['medium']) . '" alt="' . esc_attr($hotel_image2['alt']) . '" class="custom-image-class" />';
        }
        if ($hotel_image3) {
            echo '<img src="' . esc_url($hotel_image3['sizes']['medium']) . '" alt="' . esc_attr($hotel_image3['alt']) . '" class="custom-image-class" />';
        }
        echo '</div>';
    }
    ?>
</div>


        

    <?php

endwhile;
?>
    </article>

This is the code I added to the child theme’s functions.php

function enqueue_slick_slider() {
    wp_enqueue_style('slick', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.css');
    wp_enqueue_script('slick', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js', array('jquery'), '', true);
}
add_action('wp_enqueue_scripts', 'enqueue_slick_slider');


function initialize_slick_slider() {
    ?>
    <script>
    jQuery(document).ready(function($) {
        $('.hotel-images-slider').slick({
            dots: true,
            infinite: true,
            slidesToShow: 1,
            slidesToScroll: 1
            // Add more configuration options as needed
        });
    });
    </script>
    <?php
}
add_action('wp_footer', 'initialize_slick_slider');

P粉151720173P粉151720173172 days ago390

reply all(1)I'll reply

  • P粉310754094

    P粉3107540942024-04-04 10:01:49

    Put your images into the <div> itself, you currently only have one <div> with multiple images in it:

    '; echo '' . esc_attr($hotel_image['alt']) . ''; echo '
    '; } if ($hotel_image2) { echo '
    '; echo '' . esc_attr($hotel_image2['alt']) . ''; echo '
    '; } if ($hotel_image3) { echo '
    '; echo '' . esc_attr($hotel_image3['alt']) . ''; echo '
    '; } } ?>

    reply
    0
  • Cancelreply