WordPress.org

Ready to get started?Download WordPress

Forums

How to Have a Separate style.css for each Page Template (11 posts)

  1. Harringtondl
    Member
    Posted 5 years ago #

    Here is a good way to have a completely separate style.css for each page template that you have created. It works well for me. In addition to page.php, I had created 3 other optional page templates for my home page, a page without a sidebar and a page without posting code. I wanted a different appearance for each page. As one simple example of a need, I wanted to expand the body width to take up the space originally occupied by the old sidebar when I created a page without a sidebar. In explaining what to do, I'll use my four page templates as an example (page.php, nosidebar.php, nopostpage.php and myhomepage.php) All that is needed to do this is to create four style.css files, with each file corresponding to a page template that you have created. In my case these are style.css, nosidebar.css, nopostpage.css and myhomepage.css. Copy the original style.css three times, rename them to correspond to your page template names, then modify the body widths, fonts and colors to whatever you want.

    To install the code to link the correct css file to the correct page template, fFind the following code in your header.php file ...

    <META NAME="GENERATOR" CONTENT="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->

    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

    Next add the following 4 nearly-identical blocks of code just after the <link rel line in above coding .... This will assign a different css file for each of your pages.

    <?php
    if ( is_page_template('page.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/style.css" />
    <?php } ?>

    <?php
    if ( is_page_template('nosidebar.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/nosidebar.css" />
    <?php } ?>

    <?php
    if ( is_page_template('nopostpage.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/nopostpage.css" />
    <?php } ?>

    <?php
    if ( is_page_template('myhomepage.php')) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/myhomepage.css" />
    <?php } ?>

    Final note ... If you have 6 total page templates that you are using (the original page.php plus 5 optional page templates that you have created), then you will add 6 blocks of code above instead of 4. As it has done for me, this should provide you with great flexibility in your page layouts.

    DLH01 Dave Harrington
    Troy, Michigan USA

  2. Grumbledook
    Member
    Posted 5 years ago #

    I was looking at how to do this, though isn't this overkill? I want to remove the sidebars for gallery pages to fit more thumbnails across the width.

    So I've made a new page template as you have done. Though surely you can just remove the sidebar code and then change the names of the css classes. Then just put all the new css in the existing stylesheet.

    Would that not work, or did you have some other reason for not doing so. Perhaps there would be complications if you were adjusting the body width, though that could be solved by nesting a div inside it and putting a css class on that instead.

  3. Milo
    Member
    Posted 5 years ago #

    Generic WordPress Body Tag

    <body id="<?php echo the_slug(); ?>"<?php global $post; if (is_front_page()) {echo ">";} else {?> class="sub">

  4. Harringtondl
    Member
    Posted 5 years ago #

    To grumbledook: I can see where some might view it as overkill, but it is straightforward, and really doesn't ask for much to eat. It gives you complete flexibility to do whatever you want to make these pages different, either now or in the future, including the header, sidebar or body width. I stretched some of my pages out to 1000 px from the original 750 px. Once you have the multiple stylesheets in place you can be sure that any changes you make to one dedicated stylesheet will not affect the other pages, only the ones assigned to it. You mentioned that you created a new page template, but you did not indicate whether this method worked. Did it work for you?
    Harringtondl

  5. t31os
    Member
    Posted 5 years ago #

    You could use a case/switch for this to, and use default to include the default CSS file...

    Could proberly reduce the amount of code you need slightly to by doing that... though i'm not sure which is faster, case/switch or if/else, there's proberly not much in it.

  6. gabi_cavaller
    Member
    Posted 5 years ago #

    Very logical method of carrying this out, same as above, I was hoping there would be a simpler method in order to carry out such basic requirements.

    Hate bringing this up but with joomla, you can quickly and easily over ride the default theme with anything else you have pre-made with a simple drop down box. You can choose it to simply modify that page and or any other sub pages following that.

    Thank you for sharing that though, taken me a while to come accross it,

    Thanks,

    Gabi

  7. zartgesotten
    Member
    Posted 5 years ago #

    Wouldn't conditional Tags work? I'm not sure they work outside of the loop but you try. Inside the header
    is_page(xy) ... stylesheet1.css
    is_home ....... stylesheed2.css

    Just an idea. Too tired to try...
    Anja

  8. gabi_cavaller
    Member
    Posted 5 years ago #

    Hi,

    The annoying this is you have to choose the destination page in order to verify the CSS.

    What if say I have a directory called blue, and I wish for all the subsequent pages within that directory to inherit the "blue.css" style sheet instead of listing all the pages within the directory as an if statement which will constantly need to be updated?

    Any ideas or suggestions would be very welcomed and if you are in London I will buy you a couple of drinks for the functional solution :)

  9. wahhadesign
    Member
    Posted 5 years ago #

    I have just started working with wordpress. I am not quite getting this post. I was looking for more details. In my header.php I put in the exact coding you have, of course I changed your .php to reflect mine. Am I suppose to change where it says"is_page_template"? Anything else I need to put in other pages. My main question is in my secondpage.php file I have the same as the regular .php file, but how do I tell the secondpage.php to call on the that .php.

  10. Mariam Talakhadze
    Member
    Posted 4 years ago #

  11. ianatkins
    Member
    Posted 4 years ago #

    On the latest version of wordpress it puts very useful css class's onto the body tag.

    In effect you can do all of what you need just with css.

    .page-id-1 #sidebar {display:none;}
    .page-id-2 #sidebar {display:block;}

    Having the class on the body tag makes it easy to overide any css on other pages.

    Another approach, hope it helps. Oh and check out firebug to help with the css.

    Ian.

Topic Closed

This topic has been closed to new replies.

About this Topic