首选方法是使用add_action( \'wp_enqueue_scripts\', \'themeslug_enqueue_style\' ); 在您的孩子主题中functions.php 文件或直接在referral.php 模板页面。不管怎样,都是通过使用add_action 而不是在<link> 标签
注册您的样式也是一种推荐做法。我在您的案例中看到的一个好处是,您可以在functions.php 对于每个文件,都有它们的依赖项,那么您只需在引用页面中将一个句柄排队,WP将加载注册为您的依赖项的所有其他文件referral-style.css.
例如,在functions.php
function themeslug_enqueue_style() {
wp_register_style( \'referral-style\', get_stylesheet_directory_uri() . \'path/to/your/css-file\', array( \'theme-stylesheet\' ) );
wp_register_style( \'theme-stylesheet\', get_stylesheet_uri() );
// load active theme stylesheet in all cases
wp_enqueue_style( \'theme-stylesheet\' );
}
add_action( \'wp_enqueue_scripts\', \'themeslug_enqueue_style\' );
并且在
referral.phpfunction my_referral_enqueue_style() {
wp_enqueue_style( \'referral-style\' ); // will load theme-stylesheet automatically
}
add_action( \'wp_enqueue_scripts\', \'my_referral_enqueue_style\' );
当然你可以在
referral.php 并且仍然在
functions.phpif( is_page( \'referral\' ) ) {
wp_enqueue_style( \'referral-style\' );
}
但我想向你展示两种方式