我有一个在旋转木马中显示最新WooCommerce产品的短代码,但是我希望最终用户能够在同一页面上多次使用该短代码,目前这种情况发生时,jQuery旋转木马存在冲突。
这是我用于短代码的代码,
function recent_products_slider_func($atts) {
global $woocommerce_loop;
static $count = 0;
if (empty($atts)) return;
extract(shortcode_atts(array(
\'title\' => \'Recent Products\',
\'order\' => \'DESC\',
\'orderby\' => \'date\',
\'mousewheel\' => \'false\',
\'autoscroll\' => \'1\',
\'swipe\' => \'false\',
\'scroll\' => \'1\',
\'items\' => 6
), $atts));
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => $items,
\'ignore_sticky_posts\' => 1,
\'orderby\' => $orderby,
\'order\' => $order,
\'meta_query\' => array(
array(
\'key\' => \'_visibility\',
\'value\' => array(\'catalog\', \'visible\'),
\'compare\' => \'IN\'
)
)
);
wp_enqueue_script(\'owlcarouselcustom\', get_template_directory_uri() . \'/includes/pixelstores/shortcodes/js/\' . \'owlcarousel.js\');
wp_localize_script(\'owlcarouselcustom\', \'carouselvars\', array(
\'autoscroll\' => $autoscroll
)
);
ob_start();
$products = new WP_Query( $args );
if ( $products->have_posts() ) : ?>
<div class="row ps-carousel">
<div class="col-xs-10">
<h3><?php echo $title; ?></h3>
</div>
<div class="col-xs-2">
<div class="ps-carousel-btns">
<a class="btn prev"><i class="fa fa-angle-left" /></a>
<a class="btn next"><i class="fa fa-angle-right" /></a>
</div>
</div>
</div>
<div class="row">
<div id="owl-example" class="owl-carousel">
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php if ( class_exists(\'woocommerce\') ) { woocommerce_get_template_part( \'content\', \'product\' ); } ?>
<?php endwhile; ?>
</div>
</div>
<?php endif;
wp_reset_query();
$count++;
return ob_get_clean();
}
add_shortcode(\'recent_products_slider\', \'recent_products_slider_func\');
对于jQuery,我使用以下命令:,jQuery(document).ready(function($) {
var settingObj = carouselvars;
var owlcontainer = $("#owl-example");
if(settingObj.autoscroll == 1) {settingObj.autoscroll = true;} else {settingObj.autoscroll = false;}
$(owlcontainer).owlCarousel({
autoPlay: settingObj.autoscroll,
});
});
我知道为什么这样做不起作用,但不确定什么是最好的解决方案,wp\\u localize\\u脚本中的“carouselvars”句柄在没有唯一名称的情况下被调用,因此变量被调用了两次。非常感谢任何解决方案。
亲切的问候