WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using PHP IF statement to change stylesheets (5 posts)

  1. Matt Hancock
    Member
    Posted 3 years ago #

    At the moment, I have 2 separate stylesheets that I'm using for a website.

    One of the standard style.css and another is homepage.css which is being used just for the 'Home' page instead WP.

    So I'm using the two pieces of code to call them:

    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
    <?php if (is_page('Home')) { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/homepage.css" type="text/css">
    <?php }; ?>

    Naturually, this is causing some clashes with the 2 stylesheets and I also think it looks a tad messy.

    Is it possible to write a statement that will only use homepage.css for 'Home' and style.css for the rest of the website?

    Cheers. :)

  2. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Sure (using your code above):

    if ( ! is_home() ) { ?>
         <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
    <?php } else { ?>
         <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/homepage.css" type="text/css">
    <?php }

    Alternately:

    1) Load style.css always, and use the body.home class to change styles as necessary

    2) Create a "home.php" template file, in which you link homepage.css, rather than style.css

  3. Matt Hancock
    Member
    Posted 3 years ago #

    Chip,

    Thanks for the assistance. I tried the code snippet you provided and had limited success.

    At the moment, I have a template file called homepage.php which is assigned to 'Home' in WP. How would I achieve something along the lines of your second suggestion?

    Ideally, I would like to avoid having style.css and homepage.css loaded at the same time.

    Thanks again. :)

  4. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Try renaming "homepage.php" to "home.php" if it is being used as a blog posts index on the front page.

    Or, if you are using a Static Page on the Front Page, try renaming "homepage.php" to "front-page.php".

    Then, in this template file (whether "home.php" or "front-page.php"), omit the link to style.css, and only include the link to homepage.css.

  5. Matt Hancock
    Member
    Posted 3 years ago #

    Chip,

    Thanks again.

    I did some more scouring around and my problems were related to the recent admin bar addition in 3.1. Once I disabled it, everything worked out.

    Still, it's really interesting to know and to implement.

    Thanks for your help. :)

Topic Closed

This topic has been closed to new replies.

About this Topic