Support » Themes and Templates » different css for different templates

  • Hi,

    I’m working on a site mostly using static pages in 1.5.1 and I’d like to put some code in header.php that calls a different .css file depending on what template I have assigned to the page.

    For example:

    <style type="text/css" media="screen">@import url("http://path_to_stylesheet_directory/section_name.css");</style>

    After reading this post, I was trying to do this with:

    <?php ;

    echo "<style type=\"text/css\" media=\"screen\">@import url(\"";
    echo $_GET[bloginfo('stylesheet_directory')];
    echo "/" ;
    echo $_GET['template'];
    echo ".css\");</style>"
    ; ?>

    but realised I’d got the wrong end of the stick and this just output the theme name rather than the name of the template file assigned to the page.

    I would be really grateful if someone could tell me what it is I need to replace

    echo $_GET['template'];

    with. (If indeed I am going about this whole thing in the right way!)

Viewing 10 replies - 1 through 10 (of 10 total)
  • skippy



    this tutorial might give insight into other ways to accomplish what you’re trying to do.

    Otherwise, in your header, try get_bloginfo('stylesheet_directory'); or get_bloginfo('template_directory');.

    Just to clarify things, $_GET is a standard PHP variable that’s used to retrieve value(s) for queries passed through the HTTP GET method. For example, an url of:


    Provides the value of 100 for $_GET[‘p’] and rss2 for $_GET[‘feed’].

    bloginfo() on the other hand is a WordPress function that retrieves and displays information (mainly) from the options table in your WordPress database. It would not be used in conjunction with $_GET.

    Hi and thank you all for your responses.

    If all else fails I’ll re-jig my files so the header ends sooner and the css links are in the section-specific template files.

    In the meantime though I’m curious to see if there’s a way of doing this with conditional statements…

    If I were to use conditional statements along the lines of “if this page is allocated to the about-section template, then call this file”, how would I write that?

    I’m ok with using code like this:

    $post = $wp_query->post;
    if ( in_category('9') ) {
    include(TEMPLATEPATH . '/single9.php');
    elseif ( in_category('12') ) {
    include(TEMPLATEPATH . '/single12.php');
    elseif ( in_category('42') ) {
    include(TEMPLATEPATH . '/single42.php');
    } else {
    include(TEMPLATEPATH . '/single1.php');

    with posts in categories, but just to reiterate that I am working with pages.

    Cheers and thanks again for your help




    if is_page('About') { ... }
    elseif is_page('Links') { ... }
    elseif is_page(9) { ... }

    You can use page IDs or page titles.

    Thanks but…

    I need it to take into account not just the About page, but its children and its grandchildren *and* also allow for any extra pages my clients may decide to add at a later date!

    The rejigging the files option is starting to look more attractive 🙂

    I am not a coder, so consider it just thinking aloud 🙂
    There is this post giving a code how to have different sidebars based on different templates used:
    and there is this one below showing how to have different stylesheets for different categories:
    So, if you know more PHP than I do, maybe there is a way to take best from the two posts, and combine… tweak…
    As I said: just a thought.

    Yeah! Those posts are where all this started!

    cut, paste, tweak, try again until eventually it mutated into the thing in my first post…

    This is the code of interest in the first post moshu indicated:

    $template = $_GET['template'];
    if ($template == "user") {
    include(TEMPLATEPATH . '/user-sidebar.php');
    } elseif ($template == "dev") {
    include(TEMPLATEPATH . '/dev-sidebar.php');
    } elseif ($template == "mgmt") {
    include(TEMPLATEPATH . '/mgmt-sidebar.php');
    } else {

    But it’s the template recognition thing that seems to be tripping the whole thing up.

    As a test, I have edited it slightly to give this:

    <?php ;
    $template = $_GET['template']; //as per original
    if ($template == "about_section") { //one of my template files
    echo "about_section";
    } elseif ($template == "africa_section.php") { //incase I have the format wrong!
    echo "africa_section";
    } elseif ($template == "tfsr") { // the name of my theme directory to test something I thought I saw happening previously
    echo "tfsr";
    } else {
    echo "nothing doing";

    ; ?>

    I put this code in my footer.php file. Every page in my site now has “nothing doing” in the footer regardless of which template I have assigned it to!

    How do I get it to print on screen what template it thinks I am using so I can check what’s going on?

    I have tried things like

    <?php $template = $_GET['template'];
    echo $template;
    ; ?>

    and variations but I’m not getting anything. What syntax should I use?

    I have no idea what the code above does 🙂 – initially I was just looking at your OP: calls a different .css file depending on what template I have assigned to the page., and I know it’s Page in WP lingo that you talk about.

    So I remembered a code for the header that calls for different stylesheets for categories. But that’s not what you want. Then there is the other code that calls different sidebar templates based on page templates used.
    So, my logic was: based on the second code (=template recognition) you can call/include a different stylesheet (as in the other example). But perhaps I am wrong, since I am not a coder. Sorry, if my thoughts are misleading…

    Hi moshu, thanks for your persistance!

    No apology required: I agree entirely with your logic and it’s the same train of thought I have been following. So if it’s wrong then we’re both wrong together 🙂

    It’s just the template recognition bit that seems to be the stumbling block: I’ve not been able to get the $template = $_GET['template']; thigummy to work for me so I can then tweak it to bring in the stylesheet stuff.

    Sorry if it seemed a bit random, my last post is just me fiddling with that stuff to try and get proof that the first bit works. No joy yet!

    Maybe it’s completely the wrong approach, but it feels like it should work and my gut feeling is this sort of template conditional stuff would be a very useful tool in future work so if there is a solution I’d like to chase it for a bit longer 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘different css for different templates’ is closed to new replies.