如何从与主题 functions.php 不同的文件中取出样式?
How to dequeue styles from a different file than the theme's functions.php?
如果包含在主题的 functions.php
中,则下面的 WP 标准 CSS 出列代码可以工作。但是如果包含在另一个文件中,比如 my_theme/some_folder/some_template_file.php
那么它就不起作用(也就是说,样式表不再出队)。为什么?除了在 my_theme/functions.php
中包含代码(出于离题原因我不能这样做)之外,还有其他解决方法吗?
add_action('wp_print_styles', 'dequeue_iw_theme_styles', 100);
function dequeue_iw_theme_styles() {
wp_dequeue_style('iw');
}
以防万一,有问题的样式表是从 my_theme/functions.php
这样调用的:
wp_enqueue_style('iw', get_template_directory_uri() . '/css/iw.css', array(), filemtime( get_stylesheet_directory() . '/css/iw.css'));
我真的不知道除了创建插件或为此编写子主题之外是否存在其他方法。
编写插件对于如此小的、特定于主题的任务来说并不理想;但您始终可以编写子主题来覆盖主题中的特定内容。
在子主题的functions.php
中,您可以添加取消注册或取消排队所需脚本的代码。这样,您就可以避免触及主题的原始 functions.php
,实际上最好的做法是向 WordPress 主题添加自定义。
参考资料
- https://codex.wordpress.org/Child_Themes
- https://developer.wordpress.org/themes/advanced-topics/child-themes/#3-enqueue-stylesheet
希望对您有所帮助。干杯!
如果包含在主题的 functions.php
中,则下面的 WP 标准 CSS 出列代码可以工作。但是如果包含在另一个文件中,比如 my_theme/some_folder/some_template_file.php
那么它就不起作用(也就是说,样式表不再出队)。为什么?除了在 my_theme/functions.php
中包含代码(出于离题原因我不能这样做)之外,还有其他解决方法吗?
add_action('wp_print_styles', 'dequeue_iw_theme_styles', 100);
function dequeue_iw_theme_styles() {
wp_dequeue_style('iw');
}
以防万一,有问题的样式表是从 my_theme/functions.php
这样调用的:
wp_enqueue_style('iw', get_template_directory_uri() . '/css/iw.css', array(), filemtime( get_stylesheet_directory() . '/css/iw.css'));
我真的不知道除了创建插件或为此编写子主题之外是否存在其他方法。
编写插件对于如此小的、特定于主题的任务来说并不理想;但您始终可以编写子主题来覆盖主题中的特定内容。
在子主题的functions.php
中,您可以添加取消注册或取消排队所需脚本的代码。这样,您就可以避免触及主题的原始 functions.php
,实际上最好的做法是向 WordPress 主题添加自定义。
参考资料
- https://codex.wordpress.org/Child_Themes
- https://developer.wordpress.org/themes/advanced-topics/child-themes/#3-enqueue-stylesheet
希望对您有所帮助。干杯!