How to Have a Separate style.css for each Page Template
-
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
- The topic ‘How to Have a Separate style.css for each Page Template’ is closed to new replies.