WordPress: Custom Post Type Slider

WordPress: Custom Post Type Slider

Getting Light Slider

Download Light Slider here.

Then extract the content, copy the img, js and css in src directory into your WordPress Plugin folder.

Include Light Slider JavaScript & CSS

wp_enqueue_style( 'light-slider-css', plugin_dir_url( __FILE__ ) . 'css/lightslider.css', array(), $this->version, 'all' );
wp_enqueue_script( 'light-slider-js', plugin_dir_url( __FILE__ ) . 'js/lightslider.js', array( 'jquery' ), $this->version, false );

Add Some Padding to the item and set link to white

<style type="text/css">
  .lslide {
    min-height: 250px !important;
    padding-left: 50px !important;
    padding-right: 50px !important;
    padding-top: 25px !important;
    padding-bottom: 25px !important;
  }
  .lSSlideOuter .lSPager.lSpg > li a {
    background-color: white !important;
  }
</style>

Getting posts

$posts = wp_get_recent_posts( ['numberposts' => 5,'category_name' => 'Announcement'], OBJECT );

The View Part

<?php $index = 0; ?>

<ul id="light-slider">
  <?php foreach ($posts as $key => $value): ?>
    <li>
      <h4 style="color:white !important;font-style: bold !important;"><?= $value->post_title; ?></h4>
      <p style="color:white !important;"><?= strip_tags($value->post_content, ['p','span']); ?></p>
      <a href="<?= $value->guid; ?>" style="color:white !important;text-decoration: underline;float: right;"><?php echo (get_locale() != 'ms_MY') ? 'Read More':'Baca Lagi'; ?></a>
    </li>
    <?php $index++; ?>
  <?php endforeach ?>
</ul>

Final Part: initialize the Light Slider

jQuery(document).ready(function($) {
    jQuery("#light-slider").lightSlider({auto:true,item:1,speed:1000});
});

Result

lkim-slider
lkim-slider

WordPress Post Slider available here

Leave a Reply

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

four × 2 =