ANh chị xem video để biết cách làm nha còn code thì đính kèm dưới video:
Import thư viện owlcarousel (file functions.php) nhé anh chị:
1 2 3 4 5 6 7 |
add_action( 'wp_enqueue_scripts', 'standard_load_scripts' ); function standard_load_scripts() { wp_enqueue_script( 'owl-carousel-js', get_stylesheet_directory_uri(). '/js/owl.carousel.js', array( 'jquery' ), '1.0.0',false ); wp_enqueue_style( 'add-owl-carousel-css', get_stylesheet_directory_uri(). '/js/owl.carousel.css', array(),'1.0.0' ); } |
Code widget đây:
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
<?php class slide_product_cat extends WP_Widget { function __construct(){ $widget_ops = array('description' => 'Home - Featured Product Category'); $control_ops = array('width' => 300, 'height' => 300); parent::__construct(false,$name='Home - Featured Product Category',$widget_ops,$control_ops); } function form($instance){ global $wpdb; //Defaults $instance = wp_parse_args( (array) $instance, array('title'=>'','product_ids'=>'') ); $title = htmlspecialchars($instance['title']); $product_ids = htmlspecialchars($instance['product_ids']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: </label> <input name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo $title; ?>"> </p> <p><label for="<?php echo $this->get_field_id('product_ids'); ?>">Category IDs: </label> <input name="<?php echo $this->get_field_name('product_ids'); ?>" id="<?php echo $this->get_field_id('product_ids'); ?>" value="<?php echo $product_ids; ?>"> </p> <?php } /*Saves the settings. */ function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = stripslashes($new_instance['title']); $instance['product_ids'] = stripslashes($new_instance['product_ids']); return $instance; } function widget($args, $instance) { global $wpdb,$post; extract($args); $term_ids = explode(",", $instance['product_ids']); echo $before_widget; ?> <h2><span><?php echo $instance['title']; ?></span></h2> <div class="home-slide-<?php echo $term_ids[0]; ?> owl-carousel owl-theme"> <?php $args = array( 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $term_ids, ) ), 'posts_per_page' => -1, ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ): while ( $the_query->have_posts() ): $the_query->the_post(); ?> <div class="product-item-custom type-product"> <?php do_action("woocommerce_before_shop_loop_item"); do_action("woocommerce_before_shop_loop_item_title"); do_action("woocommerce_shop_loop_item_title"); do_action("woocommerce_after_shop_loop_item_title"); do_action("woocommerce_after_shop_loop_item"); ?> </div> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> </div> <script> jQuery(function($){ $(document).ready(function() { var owl = $('.home-slide-<?php echo $term_ids[0]; ?>'); owl.owlCarousel({ loop: true, margin: 10, autoplay:true, autoplayTimeout:3000, autoplayHoverPause:true, responsiveClass: true, dotsEach: true, dotData: true, nav: true, responsive: { 0: { items: 1, nav: false, }, 449: { items: 2 }, 650: { items: 2 }, 768: { items: 3 }, 1000: { items: 4, } } }); owl.on('mousewheel', '.owl-stage', function (e) { if (e.deltaY>0) { owl.trigger('next.owl'); } else { owl.trigger('prev.owl'); } e.preventDefault(); }); }); }) </script> <?php echo $after_widget; } }// end class register_widget('slide_product_cat'); //add class woocommerce add_filter( 'body_class', 'custom_class' ); function custom_class( $classes ) { $classes[] = 'woocommerce'; return $classes; } //cái này là thêm excerpt dưới title thôi add_action("woocommerce_shop_loop_item_title","show_subtitle_below_title",11); function show_subtitle_below_title(){ global $post; ?> <p class="sub-title"><?php echo custom_excerpt(get_the_content($post->ID),4,'...'); ?></p> <?php } remove_action("woocommerce_after_shop_loop_item","woocommerce_template_loop_add_to_cart",10); ?> |
Mọi sự sao chép xin ghi rõ nguồn là fcwordpress.net
Chuyên trang về wordpress: hướng dẫn thiết kế theme, plugin, thủ thuật wordpress