我在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' );
}
我终于弄清楚了如何做到这一点:
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()
删除添加到插件中的if
,然后将以下代码添加到function.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(),如果我们在存档页面等,则需要形成脚本。