我在class_wc_frontend_scripts.php中添加了一个if语句,这是woocommerce插件的核心文件,因此不应该自行更改。所以我需要将整个事情移到functions.php,以保持woocommerce的可更新性。
我想我必须以某种方式将if语句加载到动作钩子'wp_enqueue_scripts'并使其扩展现有的函数或类,但无法弄清楚这是如何完成的......
有什么想法吗?
public static function load_scripts() {
global $post;
// Load gallery scripts on product pages only if supported.
// THIS ONE IS HERE BY DEFAULT AND STAYS
if ( is_product() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) {
if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
self::enqueue_script( 'zoom' );
}
if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
self::enqueue_script( 'flexslider' );
}
if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
self::enqueue_script( 'photoswipe-ui-default' );
self::enqueue_style( 'photoswipe-default-skin' );
add_action( 'wp_footer', 'woocommerce_photoswipe' );
}
self::enqueue_script( 'wc-single-product' );
}
// Load gallery scripts on archive pages only if supported.
// >> THIS ONE I ADDED. IT NEEDS TO BE MOVED TO FUNCTIONS.PHP <<
if ( is_archive() || ( ! empty( $post->post_content ) && strstr( $post->post_content, '[product_page' ) ) ) {
if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
self::enqueue_script( 'zoom' );
}
if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
self::enqueue_script( 'flexslider' );
}
if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
self::enqueue_script( 'photoswipe-ui-default' );
self::enqueue_style( 'photoswipe-default-skin' );
add_action( 'wp_footer', 'woocommerce_photoswipe' );
}
self::enqueue_script( 'wc-single-product' );
}
答案 0 :(得分:0)
删除添加到插件中的if
,并将以下代码添加到主题的functions.php中:
add_action( 'wp_enqueue_scripts', 'gallery_scripts' );
function gallery_scripts() {
global $post;
if ( ! did_action( 'before_woocommerce_init' ) ) {
return;
}
if ( is_archive() ) {
if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
wp_enqueue_script( 'zoom' );
}
if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
wp_enqueue_script( 'flexslider' );
}
if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
wp_enqueue_script( 'photoswipe-ui-default' );
wp_enqueue_script( 'photoswipe-default-skin' );
add_action( 'wp_footer', 'woocommerce_photoswipe' );
}
wp_register_script( 'wc-single-product', '', array( 'jquery' ), WC_VERSION, true );
wp_enqueue_script( 'wc-single-product' );
}
}
此代码在wp_enqueue_scripts
事件时触发。代码检查is_archive(),如果我们在归档页面等,则将所需的脚本排队。
答案 1 :(得分:0)
我终于想出了自己如何做到这一点:
add_action( 'wp_enqueue_scripts', 'gallery_scripts', 20 );
function gallery_scripts() {
if ( is_archive()) {
if ( current_theme_supports( 'wc-product-gallery-zoom' ) ) {
wp_enqueue_script( 'zoom' );
}
if ( current_theme_supports( 'wc-product-gallery-slider' ) ) {
wp_enqueue_script( 'flexslider' );
}
if ( current_theme_supports( 'wc-product-gallery-lightbox' ) ) {
wp_enqueue_script( 'photoswipe-ui-default' );
wp_enqueue_style( 'photoswipe-default-skin' );
add_action( 'wp_footer', 'woocommerce_photoswipe' );
}
wp_enqueue_script( 'wc-single-product' );
}
}
这会在存档页面上加载与产品页面相同的woocommerce gallery功能。感谢Kagg Design,他实际上给我带来了使用wp_enqueue_script()