Support » Fixing WordPress » js in header on certain page only

  • Hi,
    I am trying include a javascript file in my header.php file, but for a certain page only (a page with the slug ‘band’).

    I have roughed out the code (my php isn’t too hot), but it is not working correctly, where am I going wrong?
    <?php if (is_page('band')) {
    echo "<script language="JavaScript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/ttca.js"></script>";
    } ?>

    Many thanks,

Viewing 4 replies - 1 through 4 (of 4 total)
  • you have to escape the characters. For example, echo “<script language=\”JavaScript\” type=\”text/javascript\” src=\”…\”>

    Just wondering if this worked for you JayK, as I have the same problem, and I am going to try this this weekend:)

    Dam. It didn’t work.

    Are there any plugins for this?

    Just wondering:)

    First off, you don’t do <?php ?> when you’re already inside PHP. Second, it’s easier to do one of these two.

    Using single quotes for the echo so that you don’t have to escape the double quotes and using the . joiner or whatever it’s called:

    <?php if (is_page('band')) {

    echo '<script language="JavaScript" type="text/javascript" src="' . bloginfo('stylesheet_directory') . '/ttca.js"></script>';

    } ?>

    Or you can just step out of PHP:

    <?php if (is_page('band')) : ?>

    <script language="JavaScript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/ttca.js"></script>

    <?php endif; ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘js in header on certain page only’ is closed to new replies.