Php Wordpress报纸子主题加载CSS两次
我使用Wordpress与报纸主题(作为儿童主题)。我注意到来自子主题的自定义CSS被加载了两次 我检查了function.php,我想知道第二个函数是否是问题的根源Php Wordpress报纸子主题加载CSS两次,php,css,wordpress,child-theming,Php,Css,Wordpress,Child Theming,我使用Wordpress与报纸主题(作为儿童主题)。我注意到来自子主题的自定义CSS被加载了两次 我检查了function.php,我想知道第二个函数是否是问题的根源 if ( !function_exists( 'chld_thm_cfg_parent_css' ) ): function chld_thm_cfg_parent_css() { wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_t
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 1001 );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'chld_thm_cfg_parent','td-theme','td-theme-demo-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css' );
谢谢你的帮助
编辑1:我通过站点代码检查器找到了这两行代码。你认为可能就是这个吗
<link rel='stylesheet' id='td-theme-css' href='.../wp-content/themes/Newspaper-lesser-old-child/style.css?ver=8.1.1504096948' type='text/css' media='all' />
<link rel='stylesheet' id='chld_thm_cfg_child-css' href='.../wp-content/themes/Newspaper-lesser-old-child/style.css?ver=8.1.1504096948' type='text/css' media='all' />
我们可以看到样式表包含了两次不同的ID…:
<link rel='stylesheet' id='td-theme-css'
href='.../wp-content/themes/Newspaper-lesser-old-child/style.css?ver=8.1.1504096948' type='text/css' media='all' />
<link rel='stylesheet' id='chld_thm_cfg_child-css'
href='.../wp-content/themes/Newspaper-lesser-old-child/style.css?ver=8.1.1504096948' type='text/css' media='all' />
现在我们需要追踪另一个实例。这就是困难所在
因为您没有包含来自父级的排队代码
潜在原因和解决方案:
get\u stylesheet\u directory\u uri
td主题
来自报纸主题。这表明它正在使用get\u stylesheet\u directory\u uri
而不是get\u template\u directory\u uri()
来加载样式表
get\u stylesheet\u directory\u uri
从活动主题的文件夹加载样式表,因此如果该主题的子项处于活动状态,则它将加载子样式表而不是自己的样式表
如果它已经由父主题加载,则不需要在子主题中再次加载它,因此在child functions.php中删除再次加载子样式表的代码
这个问题还有其他潜在的原因,但它们在这里似乎不适用。不过,我会把它们包括在内,以防你的问题没有所有排队功能,也包括可能有类似问题的其他人
wp_enqueue_style( 'td-theme', get_stylesheet_directory_uri() . '/style.css',
array(), $version);
wp_enqueue_style( 'chld_thm_cfg_child', get_stylesheet_directory_uri() . '/style.css',
array(), $version);
在您的例子中,这似乎不是问题,因为您使用不同的id(“chld_thm_cfg_parent”)来加载父样式表
如果是这种情况,要解决它,您应该对父样式表使用get\u template\u directory\u uri
,对子样式表使用get\u directory\u uri
,例如:
/* load parent stylesheet from parent theme folder with get_template_directory_uri */
wp_enqueue_style( "td-theme", get_template_directory_uri() . '/style.css' ,
array(), $version);
/* load child stylesheet from child theme folder withget_stylesheet_directory_uri
Note: we include the parent's id in the dependencies array so it gets loaded first */
wp_enqueue_style( "chld_thm_cfg_child", get_stylesheet_directory_uri() . '/style.css',
array("parent-id"), $version);
(请注意,这可能会导致父主题被包含两次,具体取决于它在父主题中的包含方式-但同样,在没有看到实际代码的情况下,很难准确地知道)。它不是由您发布的代码加载的,因此它必须在其他地方加载。我们无法知道在哪里不看到你的代码的其余部分,所以你必须试着调试你的网站来找到它。好吧,我不知道,我应该先看看哪里。有什么建议吗?我已经编辑了这篇文章。你认为这两行可能是它吗?那该怎么办呢?是的,这两行包含相同的样式表,并且加载了两次!您的代码包括其中一个,但如果没有看到父主题的排队代码,我不知道另一个来自哪里。我在下面列出了一个答案和一些可能的解决方案——如果您有任何问题或更多信息要补充,请告诉我。或者,如果答案对你的问题有帮助,你可以接受它,这样你的问题就会在网站上被标记为已解决,其他有类似问题的用户也会知道这对他们也有帮助。这也会给我们两个代表点:)你好,谢谢你的回答!我在child funcion.php中对enquence部分进行了注释。现在看起来不错。但奇怪的是,我找不到父主题在哪里加载这个td主题css。我可能会通过整个模板文件夹找到它,因为我很好奇。再次感谢您的帮助。@p31r很高兴我能帮忙!在这种情况下,听起来您的父主题正在使用
get\u stylesheet\u directory\u uri
加载子样式表。是的,当你找不到这样的东西时,它会很烦人,即使它还能工作!祝你好运找到它:)
/* load parent stylesheet from parent theme folder with get_template_directory_uri */
wp_enqueue_style( "td-theme", get_template_directory_uri() . '/style.css' ,
array(), $version);
/* load child stylesheet from child theme folder withget_stylesheet_directory_uri
Note: we include the parent's id in the dependencies array so it gets loaded first */
wp_enqueue_style( "chld_thm_cfg_child", get_stylesheet_directory_uri() . '/style.css',
array("parent-id"), $version);