In my child theme's functions.php I wrote :

function add_js() {

   echo "path is : ".get_stylesheet_directory_uri().'/js/script.js';
   wp_enqueue_script( 'script', get_stylesheet_directory_uri().'/js/script.js', array('jquery'), '1.0', true );

add_action('wp_enqueue_scripts', 'add_js');

where script.js contains this dummy script : alert("something");

This script works everywhere except for my template pages. Though the path displayed thanks to the echo is correct. I noticed that while I was trying to call my script only from template pages, thanks to is_page_template() conditional tag.

My page templates are located at the root of my child theme's folder, I guess this is the source of the issue but I can't explain why. Any idea please ?

  • Where did you add is_page_template() ? – Sumit Apr 23 '16 at 15:27
  • I added it in the beginning but as I had the same problem as stated above, I removed it. In its actual form, shouldn't my code work for every kind of page/post ? – Fafanellu Apr 23 '16 at 15:28
  • 1
    Try including script in header instead of footer by making last parameter as false ! – Sumit Apr 23 '16 at 15:52
  • 1
    Do your template pages contain the wp_footer function to output the scripts? – Milo Apr 23 '16 at 15:55
  • 1
    Solved thanks to you ! As I tested with a "basic" template it worked, then I noticed I hadn't any get_footer() ! Now it works for both "true" and "false" parameters, and is loaded in the header or the footer. Thank you both ! – Fafanellu Apr 23 '16 at 15:58

Your Answer

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

Browse other questions tagged or ask your own question.