0

I'm trying to add styles and scripts to specific template, But it's not working.

In functions.php I tried:

  • if( is_page_template('file.php') ){}

  • if( is_page_template( get_template_directory() . 'file.php') ){}

  • if( is_page_template( get_template_directory_uri() . 'file.php') ){}

When I searched for that, Some answers suggested using wp_reset_query();, Before the condition, but didn't work too.

Also others suggested using the condition inside a function:

function test(){
    if( is_page_template( get_stylesheet_directory() . 'file.php' ) ){
        wp_enqueu_script( 'custom-script', get_template_directory_uri() . 'js/custom.js' );
    }
}

add_action( 'wp_enqueue_scripts', 'test' );

Nothing worked with me.

In file.php:

<?php
/* 
    Template Name: testing
*/
?>

I created a page and used that template.

What's the problem?

0

You have wp_enqueu_script within your function spelled wrong. It should be wp_enqueue_script.

Also is_page_template() will look for your template file as is relative to your theme directory. If file.php is in the root of your theme then it would just be is_page_template('file.php'). Here is the reference for that function: https://developer.wordpress.org/reference/functions/is_page_template/

Try this:

function test(){
    if( is_page_template( 'file.php' ) ){
        wp_enqueue_script( 'custom-script', get_template_directory_uri() . 'js/custom.js' );
    }
}

add_action( 'wp_enqueue_scripts', 'test' );

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.