0

This doesn't work for me in functions.php:

    if( is_page_template( 'template-flat.php' ) ) {

function flatsome_scripts() {
wp_enqueue_style( 'flatsome-style', get_template_directory_uri() .'/flatash/css/foundation.css', array(), '2.1', 'all');

}

add_action( 'wp_enqueue_scripts', 'flatsome_scripts' );

    }

However, if I include the style manually in the header of template-flat.php like this it works.

<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/flatash/css/foundation.css" type="text/css" /> 

Why is my functions code not working?

8
  • 1
    Is your code wrapped inside a function hooked to wp_enqueue_scripts? Commented Feb 5, 2015 at 18:05
  • 1
    Are you hooking the code in functions.php in any action hook? If so, please, show us. Also, you have mentioned "header" of template-flat.php, please show us also the header of this file. Commented Feb 5, 2015 at 18:05
  • 1
    Are you calling wp_enqueue_style within a function? Commented Feb 5, 2015 at 18:05
  • 1
    WOW, all of us asking almost the same at the same time ;) Commented Feb 5, 2015 at 18:06
  • 1
    Take a glance at the Action Reference. The code in functions.php executes at after_setup_theme, however conditionals do not function properly until posts_selection. The wp_enqueue_scripts action occurs after all of the above, meaning code executed here can use conditionals. Long story short, move your if( is_page_template( 'template-flat.php' ) ) conditional inside of flatsome_scripts() Commented Feb 5, 2015 at 18:47

3 Answers 3

4

Your problem is that you have wrapped your complete function and your action inside your conditional check. Your conditional check for your page template should be in your function.

Page templates gets set quite late, too late for your action.

Your function should look like this

function flatsome_scripts() 
{

    if( is_page_template( 'template-flat.php' ) ) {
        wp_enqueue_style( 'flatsome-style', get_template_directory_uri() .'/flatash/css/foundation.css', array(), '2.1', 'all');
    }

}

add_action( 'wp_enqueue_scripts', 'flatsome_scripts' );
0

You do it, the same way you do it to register other scripts, put it in a function and then add then use add_action. This goes in functions.php.

function register_styles() {

    wp_enqueue_style( 'flatsome-style', get_template_directory_uri() .'/flatash/css/foundation.css', array(), '2.1', 'all');

}

add_action('wp_enqueue_scripts','register_styles');
0

What happens if you place the conditional inside the function, like:

function flatsome_scripts() {
    if( is_page_template( 'template-flat.php' ) ) {
        wp_enqueue_style( 'flatsome-style', get_template_directory_uri() .'/flatash/css/foundation.css', array(), '2.1', 'all');
    }
}
add_action( 'wp_enqueue_scripts', 'flatsome_scripts' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.