wordpress - WooCommerce 中用于追加销售产品的光滑滑块
问题描述
我需要为向上销售的产品制作一个轮播。为此,我使用slick-slider。
脚本连接:
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.9.0/slick/slick.css"/>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.9.0/slick/slick-theme.css"/>
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/kenwheeler/slick@1.9.0/slick/slick.min.js"></script>
脚本初始化:
$(document).ready(function() {
$('.multiple-items').slick({
infinite: true,
autoplay: true,
slidesToShow: 4,
slidesToScroll: 4
});
});
up-sells.php 文件中的代码:
if ( $upsells ) : ?>
<section class="up-sells upsells products">
<h2><?php esc_html_e( 'You may also like…', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php foreach ( $upsells as $upsell ) : ?>
<?php
$post_object = get_post( $upsell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product-carousel' );
<?php endforeach; ?>
<?php woocommerce_product_loop_end(); ?>
</section>
<?php endif;
wp_reset_postdata();
更新:文件内容-产品-carousel.php:
<div class="carousel"><div class="multiple-items">
<div <?php post_class( $classes ); ?>>
<?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>
</div></div>
CSS:
.slick-slider {width: 100%; float: left;}
.slick-slide {cursor: pointer;}
.multiple-items .slick-slide {margin: 0 15px;}
.carousel {padding: 0 3%; float: left; width: 100%; box-sizing: border-box;}
不幸的是,我无法制作产品轮播。产品显示在一列中,滑块脚本不想工作。我需要你的帮助。如何使轮播产品向上销售?
解决方案
您将代码放在错误的位置,slack 被多次初始化。您应该将“carousel”和“multiple-items”divs放在循环之外,否则它会为每个元素初始化。正确的代码如下:
content-product.php - 默认,不需要任何自定义
up-sells.php
<section class="up-sells upsells products">
<?php
$heading = apply_filters( 'woocommerce_product_upsells_products_heading', __( 'You may also like…', 'woocommerce' ) );
if ( $heading ) :
?>
<h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<div class="carousel">
<div class="multiple-items">
<?php foreach ( $upsells as $upsell ) : ?>
<?php
$post_object = get_post( $upsell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found
wc_get_template_part( 'content', 'product' );
?>
<?php endforeach; ?>
</div>
</div>
<?php woocommerce_product_loop_end(); ?>
</section>
推荐阅读
- python - 我有一个 XML 文档,我正在解析它以检索多个 getElementsByTagName 数据
- javascript - Javascript:解析 JSON 字符串
- python - 如何在python / numpy中先按实部然后按虚部对特征值进行排序
- r - 100x 循环,从数据帧 A 中获取行名,每次作为一列添加到新的数据帧 B,以 B 中的 100 列结束
- python - 如何从第一个元素开始逐个打印列表中的元素并递增直到python中的最后一个
- c++ - 在 C++ 中,如何将字符串输入到结构的 char 数组中?
- c++ - 一旦玩家达到目标,OpenGL会重绘对象
- haskell - 如何将麦克风数据流式传输到我的 Haskell 程序中?
- r - 在r中汇总data.frame的多个变量的数据?
- java - Spring Boot ApplicationRunner 在启动期间运行 - 而不是之后