如何根据模板名称添加自定义CSS和JS文件?例如,我的普通页面都将有一个绿色的背景色。现在我想要这个Blue Template
将有一个额外的CSS文件完成的蓝色背景色。我该怎么做?
Blue.php
<?php
/*
Template Name: Blue Template
*/
?>
<?php get_header(); ?>
<div class="container">
<h1>This template has an blue background!</h1>
</div>
<?php get_footer(); ?>
functions.php
<?php
function load_theme_files() {
# Green background - Default Stylesheet, all pages need this one
wp_enqueue_style(\'style\', esc_url( get_stylesheet_directory_uri() ).\'/style.css\');
# What I need - This should add blue.css to the header
if ($currentTemplate == \'Blue Template\') {
wp_enqueue_style(\'blue\', esc_url( get_stylesheet_directory_uri() ).\'/blue.css\');
}
}
add_action(\'init\', \'load_theme_files\');
?>
这应该给所有正常的页面<link href="/style.css" rel="stylesheet" type="text/css".
我的蓝色模板将有蓝色。css低于我的正常样式。css。我试过了if ( is_page_template( \'blue.php\' ) ) { # HERE THE BLUE CSS }
但这没有任何作用。