在 Woocommerce 中更改移动设备上的 FlexSlider 选项
Change FlexSlider options on mobile in Woocommerce
默认在单个产品页面上启用选项 'controlNav' = 'thumbnails'。台式机没问题。但是在手机上我想 'controlNav' = true (dots)。我尝试使用 ajax 来做到这一点,但我认为我需要以某种方式使用 flex 幻灯片刷新该片段以应用过滤器。看不懂。
在 JS 文件中:
if (window.matchMedia('(max-width: 800px)').matches) {
$.ajax({
url: wc_add_to_cart_params.ajax_url,
data: { action: 'mobile_slider'},
type: 'POST'
})
.then(res => console.log('works', res))
}
在functions.php中:
function hellenik_change_slider_mobile()
{
add_filter('woocommerce_single_product_carousel_options', 'hellenik_update_woo_flexslider_options');
function hellenik_update_woo_flexslider_options($options)
{
$options['smoothHeight'] = true;
$options['controlNav'] = true;
$options['animation'] = "slide";
$options['slideshow'] = false;
return $options;
}
wp_die();
}
add_action('wp_ajax_mobile_slider', 'hellenik_change_slider_mobile');
add_action('wp_ajax_nopriv_mobile_slider', 'hellenik_change_slider_mobile');
以下使用 WooCommerce 专用 woocommerce_single_product_carousel_options
过滤器挂钩并使用 WordPress wp_is_mobile()
条件标签:
add_filter( 'woocommerce_single_product_carousel_options', 'filter_single_product_carousel_options' );
function filter_single_product_carousel_options( $options ) {
if ( wp_is_mobile() ) {
$options['smoothHeight'] = true; // Already "true" by default
$options['controlNav'] = true; // Option 'thumbnails' by default
$options['animation'] = "slide"; // Already "slide" by default
$options['slideshow'] = false; // Already "false" by default
}
return $options;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
查看 WooCommerce related default settings for woocommerce_single_product_carousel_options
hook:
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options',
array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
'allowOneSlide' => false,
)
),
文档:WordPress Developer Resources - wp_is_mobile()
conditional function
默认在单个产品页面上启用选项 'controlNav' = 'thumbnails'。台式机没问题。但是在手机上我想 'controlNav' = true (dots)。我尝试使用 ajax 来做到这一点,但我认为我需要以某种方式使用 flex 幻灯片刷新该片段以应用过滤器。看不懂。
在 JS 文件中:
if (window.matchMedia('(max-width: 800px)').matches) {
$.ajax({
url: wc_add_to_cart_params.ajax_url,
data: { action: 'mobile_slider'},
type: 'POST'
})
.then(res => console.log('works', res))
}
在functions.php中:
function hellenik_change_slider_mobile()
{
add_filter('woocommerce_single_product_carousel_options', 'hellenik_update_woo_flexslider_options');
function hellenik_update_woo_flexslider_options($options)
{
$options['smoothHeight'] = true;
$options['controlNav'] = true;
$options['animation'] = "slide";
$options['slideshow'] = false;
return $options;
}
wp_die();
}
add_action('wp_ajax_mobile_slider', 'hellenik_change_slider_mobile');
add_action('wp_ajax_nopriv_mobile_slider', 'hellenik_change_slider_mobile');
以下使用 WooCommerce 专用 woocommerce_single_product_carousel_options
过滤器挂钩并使用 WordPress wp_is_mobile()
条件标签:
add_filter( 'woocommerce_single_product_carousel_options', 'filter_single_product_carousel_options' );
function filter_single_product_carousel_options( $options ) {
if ( wp_is_mobile() ) {
$options['smoothHeight'] = true; // Already "true" by default
$options['controlNav'] = true; // Option 'thumbnails' by default
$options['animation'] = "slide"; // Already "slide" by default
$options['slideshow'] = false; // Already "false" by default
}
return $options;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
查看 WooCommerce related default settings for woocommerce_single_product_carousel_options
hook:
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options',
array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
'allowOneSlide' => false,
)
),
文档:WordPress Developer Resources - wp_is_mobile()
conditional function