How to add custom CSS and JS files depending on the Template Name? For example my normal pages will all have a green background color. Now I want that Blue Template will have a blue background color done with an extra CSS file. How can I do that?


  Template Name: Blue Template
<?php get_header(); ?>
<div class="container">
  <h1>This template has an blue background!</h1>
<?php get_footer(); ?>


  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');

This shoud give all normal pages <link href="/style.css" rel="stylesheet" type="text/css". and my Blue Template will have blue.css below my normal style.css.

I tried if ( is_page_template( 'blue.php' ) ) { # HERE THE BLUE CSS } but this doesn't do anything.


You can use wp_enqueue_style() before running get_header() in your blue.php file.

If you're looking for a way to do that inside your functions.php file, then the init hook is a bit too early for is_page_template(). Try the wp_enqueue_scripts hook instead:

add_action( 'wp_enqueue_scripts', 'load_my_theme_files' );
function load_my_theme_files() {
    if ( is_page_template( 'blue.php' ) ) {
        wp_enqueue_style( ... );

Hope that helps!

| improve this answer | |
  • So the problem was that I used init instead of wp_enqueue_scripts. Does it matter if I use wp_enqueue_scripts or wp_enqueue_styles? I would like to call both in the same function (i.e. Both scripts and CSS files) – Jeff Apr 3 '16 at 12:25
  • There's no action called wp_enqueue_styles, you can enqueue both scripts and styles during wp_enqueue_scripts, that's what the default themes do too. – kovshenin Apr 3 '16 at 12:35
  • Ah, oké. I thought I read that somewhere. Thanks for help! :) – Jeff Apr 3 '16 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.