Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 04:38:45 PM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155544 Posts in 21714 Topics by 7736 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Snippet: include stylesheet based on page ID or parent ID  (Read 1147 times)
Argos
Moderator
**
Offline Offline

Posts: 2161


WWW
« on: January 10, 2011, 12:12:12 AM »

Normally you use different templates if you need different layouts for different pages, but if the only difference between the templates is in the CSS, you can also use stylesheets specifically for certain page ID's or parent ID's. So a kind of conditional stylesheets using php. I needed this and I thought others may find it handy to know. This is especially useful for already existing pages, and not for new ones, as you need to know the page ID's  smiley

Say you have 1 default stylesheet, and a few alternative versions. You can just put the differences with the default one in the alternative versions, and call them after the default one. Then the alternative ones will override the default one as you know. So instead of

Code:
<link rel="stylesheet" type="text/css" href="<?php echo TEMPLATE_DIR?>/screen.css"></link>
<link rel="stylesheet" type="text/css" href="<?php echo TEMPLATE_DIR?>/print.css"></link>

you can put something like this in your template:

Code:
<link rel="stylesheet" type="text/css" href="<?php echo TEMPLATE_DIR?>/screen.css"></link>
<?php
if (PAGE_ID==2||PARENT==2) { 
echo('<link rel="stylesheet" type="text/css" href="'.TEMPLATE_DIR.'/orange.css"></link>');
}
elseif (
PAGE_ID==3||PARENT==3||PAGE_ID==4||PARENT==4) { 
echo('<link rel="stylesheet" type="text/css" href="'.TEMPLATE_DIR.'/blue.css"></link>');
}
elseif (
PAGE_ID==5||PARENT==5||PAGE_ID==6||PARENT==6) { 
echo('<link rel="stylesheet" type="text/css" href="'.TEMPLATE_DIR.'/green.css"></link>');
}
?>

<link rel="stylesheet" type="text/css" href="<?php echo TEMPLATE_DIR?>/print.css"></link>

So the page with ID 2 and all its children will use orange.css. The page with ID's 3 or 4 and all their children will use blue.css, etc. Of course you can choose either parent page or child pages or both, or use other php conditions. Works like a charm  grin
« Last Edit: January 11, 2011, 02:24:32 AM by Argos » Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
PurpleEdge

Offline Offline

Posts: 232



WWW
« Reply #1 on: January 10, 2011, 01:44:56 AM »

Why not extend it even further by making your code a droplet, then you can update the droplet whenever you need to without changing the template?
« Last Edit: January 11, 2011, 02:24:47 AM by Argos » Logged
Argos
Moderator
**
Offline Offline

Posts: 2161


WWW
« Reply #2 on: January 11, 2011, 02:24:01 AM »

Why not extend it even further by making your code a droplet, then you can update the droplet whenever you need to without changing the template?

I'm no coder, I don't know how to make a droplet out of this... This snippet is about the best I can do regarding php  rolleyes
Logged

Jurgen Nijhuis
Argos Media
Heiloo, The Netherlands
WB Showcase: http://www.mywebsitebaker.com/pages/showcase.php?v&category_id=1242&count=30
----------------------------------------------------------------
Please don't request personal support, use the forums!
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!