Forums

[resolved] How do I load different style sheets to different pages? (7 posts)

  1. siegetheartist
    Member
    Posted 11 months ago #

    I have the following css in my header();.

    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">

    I know that this will only load 1 css file to all of my pages. My question is, can anyone shoot me a loop that will plug in all the correct style sheets for each individual templates? Keep in mind that all my page temples use the get_header();, get_sidebar();, and get_footer();, so the only thing changing is the content.

    So i'm guessing a need a loop in my header.php file that does the following:

    load "this.css" on "thispage.php"
    load "this_other.css" on "this_other_page.php"

    etc. etc.

    Any suggestions?

  2. Jarret Cade
    Member
    Posted 11 months ago #

    if ( is_page_template( 'thispage.php' ) ) {
         Load CSS here
        } elseif ( is_page_template( 'this_other_page.php' ) ) {
         Load CSS here
        } else {
         Load CSS here
        }

    http://codex.wordpress.org/Conditional_Tags#Is_a_Page_Template

  3. siegetheartist
    Member
    Posted 11 months ago #

    Would you please give me an example of what it would look like when "Load CSS here" is replaced with an actual path? Also, if I have more than 2 templates that I want style sheets to load on, would I just continue adding more "elseif" statements? Like this: ?

    <?php
    if ( is_page_template( 'thispage.php' ) ) {
         Load CSS here
        } elseif ( is_page_template( 'this_other_page_1.php' ) ) {
         Load CSS here
        } elseif ( is_page_template( 'this_other_page_2.php' ) ) {
         Load CSS here
        } elseif ( is_page_template( 'this_other_page_3.php' ) ) {
         Load CSS here
        } else {
         Load CSS here
        }
    ?>
  4. Jarret Cade
    Member
    Posted 11 months ago #

    Yes it would go exactly like that and where 'Load CSS here' is you would just replace that with a link to the CSS file that you would like to use for that particular page.

  5. siegetheartist
    Member
    Posted 11 months ago #

    The following is not working:

    <?php

    if ( is_page_template( 'home.php' ) ) {
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">

    } elseif ( is_page_template( 'contact.php' ) ) {
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/css/contact_form.css" media="screen" type="text/css" />

    } else {

    }

    ?>

    I'm pretty sure its because since I have opened the php and trying to write html inside of it without it being a string, it is getting confused when I try to re-open php inside of the file path. So I "think" I know what the problem is, but I don't know how to solve it. :( I thought of making it a string by putting it inside of quotes but it did nothing. Any suggestions?

  6. Jarret Cade
    Member
    Posted 11 months ago #

    <?php
    
    if ( is_page_template( 'home.php' ) ) { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
    <?php
    } elseif ( is_page_template( 'contact.php' ) ) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/css/contact_form.css" media="screen" type="text/css" />
    <?php
    } else {
    
    }
    
    ?>

    That should do it ;)

  7. siegetheartist
    Member
    Posted 11 months ago #

    Yup. That did it. And I learned something. :) I didn't know that you can open and close the php at will without effecting how it works. Thank you so much for your help. I appreciate it.

Reply

You must log in to post.

About this Topic