0

I want to add external JavaScript link in the custom post template. I am adding the below function in the child theme. But the external link is not loaded in the custom post theme. Am I doing something wrong ? TIA

function temp_enqueue_scripts() {
        if ( 'custom-template.php' == get_page_template() ) {
            wp_enqueue_script('script0', 'lib/myscript.js', array('jquery'));
        }
}
add_action( 'wp_enqueue_scripts', 'temp_enqueue_scripts' );
1

1 Answer 1

0

Add The following code (in your theme's functions.php) will register your script, and set jQuery as a dependency. So when you enqueue it, jQuery will also be enqueued.

function my_scripts_method() {
   // register your script location, dependencies and version
   wp_register_script('my_custom_script',
       get_template_directory_uri() . '/js/custom_script.js',
       array('jquery'),
       '1.0' );

}

add_action('wp_enqueue_scripts', 'my_scripts_method');

You can then enqueue your script one of two ways. In your theme's functions.php you can use the template_include filter the get the template being used using is_page_template()

function my_load_script_for_template( $template ){
     if(is_page_template('my-template.php'))
        wp_enqueue_script('my_custom_script');

return $template;
}
 add_filter( 'template_include', 'my_load_script_for_template', 1000 );

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.