• How can I add different code (js, css links etc.) to the header depending on the page?

    For example, for 404.php I would like to add something like this to the header:

    <meta content="3; URL = javascript: history.go(-1)" http-equiv="Refresh" />

    In my current, non-Wordpress site I have this in the header:

    <? echo $other ?>

    At the top of other pages using the same header I can add whatever’s necessary:

    $other = " whatever.js ";

    This solution doesn’t work with WordPress. Is there another way to do this?

    Please don’t give me alternative redirect suggestions! That’s just an example. My question is about including code in the header.

Viewing 4 replies - 1 through 4 (of 4 total)
  • For a conditional based on the page, use is_page(). For one based on the page template being used, use is_page_template().

    http://codex.wordpress.org/Conditional_Tags

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    Thanks. So you have to put all the conditional code in the header? Is that more efficent than having a “tag” in the header that pulls in conditional code from the pages? There is no way to do that?

    I’m now trying to add this to the header, but keep getting syntax errors:

    <?
    if (is_page_template('homepage.php'))
    {
    echo "<script src='<?php bloginfo("template_directory"); ?>/js/sifr.js' type='text/javascript'></script>
    <script src='<?php bloginfo("template_directory"); ?>/js/sifr-config.js' type='text/javascript'></script>";
    }
    ?>

    I’ve tried all the different combinations of ‘ and ” I could come up with. Nothing works. Am I missing something?

    This doesn’t work either:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="<?php bloginfo('template_directory'); ?>/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="<?php bloginfo('template_directory'); ?>/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>
    Thread Starter Modifiedcontent

    (@modifiedcontent)

    This solves the syntax problem:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="<?php bloginfo("template_directory"); ?>/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="<?php bloginfo("template_directory"); ?>/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>

    But this is what ends up in the rendered html:

    <script src="<?php bloginfo("template_directory"); ?>/js/sifr.js" type="text/javascript"></script><script src="<?php bloginfo("template_directory"); ?>/js/sifr-config.js" type="text/javascript"></script>

    Thread Starter Modifiedcontent

    (@modifiedcontent)

    This works:

    <?
    if (is_page_template('homepage.php'))
    {
    echo '<script src="'. get_bloginfo('template_directory').'/js/sifr.js" type="text/javascript"></script>';
    echo '<script src="'. get_bloginfo('template_directory').'/js/sifr-config.js" type="text/javascript"></script>';
    }
    ?>

    De-nested and using get_bloginfo instead of bloginfo

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Add code to header depending on template page’ is closed to new replies.