0

The below code works perfectly well thanks to this help from a previous question I posted.

The code that I've posted directly below injects CSS (or JS) into the wp_head of WordPress....

My question is: how do I make this happen on several designated templates? I thought my code listed second on this question would have worked but it is wrong.

I would like to place some CSS/ JS on specific templates in the wp_head and wp_footer

function wpse_276403_output_scripts() {
    ?>
    <script>alert('Hello world!');</script>
    <style>
        body {
            background: #F00;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'wpse_276403_output_scripts' );

My attempt below failed to load CSS into 'page-directory-2014.php'

 function wpse_276403_output_scripts() {
   if (is_page_template('page-directory-2014.php')){
    ?>
    <style>
        body {
            background: #FCFCFC;
        }
    </style>
    <?php
  }
}
add_action( 'wp_head', 'wpse_276403_output_scripts' );

Thanks for all directions!

10
  • Does it work if you use is_page_template('page-directory-2014.php') or not? I'm not sure if the question is about working in multiple templates, or not working at all.
    – Jack Johansson
    Aug 10 '17 at 8:21
  • I tried what you are saying and it crashed the site. The code that works at the top of my questions works great but it is universal. What I would like is for the top to be applied to only 3 templates...(thanks BTW Jack - you've been really helpful these last 24 hours)
    – henry
    Aug 10 '17 at 8:27
  • You're welcome :) I mean, the second code of yours. What happens when you use it? Wrong template? Error? Works? Or nothing at all?
    – Jack Johansson
    Aug 10 '17 at 8:28
  • My code crashes the entire theme/ WP installation. It might be b/c I am missing a { but I can't see where it is meant to go. What I am trying to do is apply this CSS to several unqiue templates in my theme
    – henry
    Aug 10 '17 at 8:40
  • 1
    amazing - works perfect.
    – henry
    Aug 10 '17 at 9:06

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.