这篇文章提出了一些我遇到的问题,这些问题与中提出的样式表排队方法的最新更改有关this thread 和this thread.
我遇到的问题是在一个通用用例场景中出现的,它使用了一个广泛使用且维护良好的父主题,特别是WP 4.0安装中的子主题。我的孩子主题的功能。php仅包含wp_enqueue_style
功能为detailed in the Codex.
请注意,虽然下面引用的代码特定于此主题,但其中大部分使用了父主题使用的当前编码约定。此外,我所关注的领域很可能与目前在野外建立的大量父主题重复。此外,无论使用哪个父主题,这些问题都适用于通用级别
问题1:两个排队
The Recommended Setup:
父主题正在使用wp_enqueue_scripts
挂钩,相关部分如下:add_action(\'wp_enqueue_scripts\', \'parent_theme_function_name\');
function parent_theme_function_name() {
wp_register_style( \'avia-style\' , $child_theme_url."/style.css", array(), \'2\', \'all\' );
wp_enqueue_style( \'avia-base\');
if($child_theme_url != $template_url) { wp_enqueue_style( \'avia-style\'); }
}
我的孩子主题functions.php
根据最近的codex更改对样式进行排队:add_action( \'wp_enqueue_scripts\', \'enqueue_parent_theme_style\' );
function enqueue_parent_theme_style() {
wp_enqueue_style( \'dm-parent-style\', get_template_directory_uri().\'/style.css\' );
}
请注意引用代码使用的以下ID:id=\'dm-parent-style-css\'
是父主题的样式表,由我的子主题函数查询id=\'avia-style-css\'
是my childtheme的样式表,由父主题函数排队id=\'dm-child-style-css\'
是我的子主题的样式表,由我的子主题函数排队The Results:
乍一看,一切都很好<head
> 显示以下顺序:
安装插件后,排队顺序现在更改如下:<link rel=\'stylesheet\' id=\'dm-parent-style-css\' href=\'testinstall.dev/wp-content/themes/enfold/style.css?ver=4.0\' type=\'text/css\' media=\'all\' /> <!-- Multiple individual parent theme styles here --> <link rel=\'stylesheet\' id=\'avia-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' />
最终,我需要在任何插件之后加载我的子主题的css,因此我不得不在我的子主题中的函数中添加一个优先级编号(see previous discussion regarding priority number).<link rel=\'stylesheet\' id=\'dm-parent-style-css\' href=\'testinstall.dev/wp-content/themes/enfold/style.css?ver=4.0\' type=\'text/css\' media=\'all\' /> <!-- Multiple individual parent theme styles here --> <link rel=\'stylesheet\' id=\'avia-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' /> <!-- Pesky plugin styles -->
然而,由于我的函数只将父主题的css排队,结果是现在父主题的css移到了末尾,使我的子主题的css陷入了比以前更糟糕的困境。
现在,我不得不将我的孩子主题风格也排在队列中,以确保它移回队列的最前面,从而导致the aforementioned issue twoqueueing(新术语?lol)的子主题css。<!-- Multiple individual parent theme styles here --> <link rel=\'stylesheet\' id=\'avia-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' /> <!-- Pesky plugin styles --> <link rel=\'stylesheet\' id=\'dm-parent-style-css\' href=\'testinstall.dev/wp-content/themes/enfold/style.css?ver=4.0\' type=\'text/css\' media=\'all\' />
The Deprecated Setup:
修改了子主题中的函数:add_action( \'wp_enqueue_scripts\', \'enqueue_parent_theme_style\', 99 ); function enqueue_parent_theme_style() { wp_enqueue_style( \'dm-parent-style\', get_template_directory_uri().\'/style.css\' ); wp_enqueue_style( \'dm-child-style\', get_stylesheet_directory_uri().\'/style.css\' ); }
The Results:
在中生成以下订单<head>
:
尽管在我的函数中包含子样式表导致它两次排队,但在父主题将为我们正确地将子样式表排队的假设下,这比编码更好。根据分配给每个排队样式的ID,父主题似乎将其排队,而不是WP Core中的任何内容。<!-- Multiple individual parent theme styles here --> <link rel=\'stylesheet\' id=\'avia-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' /> <!-- Pesky plugin styles --> <link rel=\'stylesheet\' id=\'dm-parent-style-css\' href=\'testinstall.dev/wp-content/themes/enfold/style.css?ver=4.0\' type=\'text/css\' media=\'all\' /> <link rel=\'stylesheet\' id=\'dm-child-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' />
My Shivm:
虽然我很难建议这是推荐的方法(而且我相信比我有更多编码经验的开发人员会对这个解决方案感到不满),但我将父主题的ID(用于将我的子主题的样式排队)排在我自己的子主题的函数文件中的排队上方,如图所示:add_action( \'wp_enqueue_scripts\', \'enqueue_parent_theme_style\', 99 ); function enqueue_parent_theme_style() { wp_enqueue_style( \'dm-parent-style\', get_template_directory_uri().\'/style.css\' ); wp_dequeue_style( \'avia-style\' ); wp_enqueue_style( \'dm-child-style\', get_stylesheet_directory_uri().\'/style.css\' ); }
The Results:
这解决了手头的问题,导致:
当然,这需要知道父主题使用的ID——需要一些更通用的东西作为标准的子主题开发方法。<!-- Multiple individual parent theme styles here --> <!-- Plugin styles --> <link rel=\'stylesheet\' id=\'dm-parent-style-css\' href=\'testinstall.dev/wp-content/themes/enfold/style.css?ver=4.0\' type=\'text/css\' media=\'all\' /> <link rel=\'stylesheet\' id=\'dm-child-style-css\' href=\'testinstall.dev/wp-content/themes/child-theme/style.css?ver=2\' type=\'text/css\' media=\'all\' />
问题2:重新定位的子样式表
(It seems hard to believe this hasn\'t come up in another thread, though I didn\'t see any specific ones when looking...if I missed it, feel free to bring it to my attention.)
我never 使用默认值style.css
在我的主题样式的子主题根目录中-它显然必须在那里,但我的所有实际样式都是从SCSS编译而来的。css文件位于/css/目录中。虽然我意识到这并不是儿童主题开发的普遍标准,但我认识的大多数严肃的WordPress开发人员都会做类似的事情。当然,这需要在我的函数中手动将该样式表排队,而不管父主题是否将其排队。总而言之style.css), 需要手动将该文件排队。就在广泛的开发人员之间保持连续性而言,鼓励手动将子样式表排队,而不管可能的重复,这难道没有意义吗