Forums

Add code to header depending on template page (5 posts)

  1. modifiedcontent
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    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

  3. modifiedcontent
    Member
    Posted 2 years ago #

    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>';
    }
    ?>
  4. modifiedcontent
    Member
    Posted 2 years ago #

    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>

  5. modifiedcontent
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic