在wordpress中为子主题排队样式表
enqueing stylesheets for a child theme in wordpress
我在 wordpress 中创建了一个名为 bigpont 的父主题的子主题。我也在网站上使用 woocommerce。我查询了我的子主题样式 sheet 并注意到它加载了两次,我不确定为什么。我也想知道如何加载它以覆盖 woocommerce 样式 sheet。这是我目前在 functions.php 文件中使用的代码:
function my_theme_enqueue_styles() {
$parent_style = 'bigpoint-css';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/base.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
这是样式sheet在我的网站上的加载方式
它似乎以 'bigpoint-default-css' 加载,然后在我添加它时再次加载 "child-style-css'
****更新:我找到了我的 css 被加载两次的答案,在我的父主题的 functions.php 文件中它被调用:
wp_register_style('bigpoint-default', get_stylesheet_uri(), '1.0');
所以我用这个来撤销那个:
function unhook_parent_style() {
wp_dequeue_style( 'bigpoint-default' );
wp_deregister_style( 'bigpoint-default' );
}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );
从查看文件 class-wc-frontend-scripts.php
来看,WooCommerce 似乎将其 scripts/styles 排入队列,默认优先级为 10。
public static function init() {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
}
因此,如果您以较低的优先级排列脚本,它们将在 WooCommerce 样式之后加载并覆盖该样式表,因为文件将在 HTML 文档下方的 WooCommerce 样式之后加载。
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
不过,我需要更多信息来调试重复样式表的情况。
我在 wordpress 中创建了一个名为 bigpont 的父主题的子主题。我也在网站上使用 woocommerce。我查询了我的子主题样式 sheet 并注意到它加载了两次,我不确定为什么。我也想知道如何加载它以覆盖 woocommerce 样式 sheet。这是我目前在 functions.php 文件中使用的代码:
function my_theme_enqueue_styles() {
$parent_style = 'bigpoint-css';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/base.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style ),
wp_get_theme()->get('Version')
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles');
这是样式sheet在我的网站上的加载方式
****更新:我找到了我的 css 被加载两次的答案,在我的父主题的 functions.php 文件中它被调用:
wp_register_style('bigpoint-default', get_stylesheet_uri(), '1.0');
所以我用这个来撤销那个:
function unhook_parent_style() {
wp_dequeue_style( 'bigpoint-default' );
wp_deregister_style( 'bigpoint-default' );
}
add_action( 'wp_enqueue_scripts', 'unhook_parent_style', 20 );
从查看文件 class-wc-frontend-scripts.php
来看,WooCommerce 似乎将其 scripts/styles 排入队列,默认优先级为 10。
public static function init() {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) );
add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 );
}
因此,如果您以较低的优先级排列脚本,它们将在 WooCommerce 样式之后加载并覆盖该样式表,因为文件将在 HTML 文档下方的 WooCommerce 样式之后加载。
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
不过,我需要更多信息来调试重复样式表的情况。