Trying to wrap my brain around this.

DC_Sara's picture

She has: 392 posts

Joined: Jan 2002

Hi,

I have been trying to figure out how to keep adding themes to this:

<?
// This is the first theme
if($theme == 'theme1')
{
$stylesheet = "theme1.css";
$headerimg = "<div id="logo">Image for theme one goes here</div>";
}

// This is the second theme
else if($theme == 'theme2')
{
$stylesheet = "theme2.css";
$headerimg = "<div id="logo">Image for theme 2 goes here</div>";
}

// This is the third theme -- also the default theme
else
{
$theme = 'theme3';
$stylesheet = "theme3.css";
$headerimg = "<div id="logo"><img src="theme3.gif" alt="logo" /></div>";
}

?>
'

I'm working on making themes for my blog that uses a certain tutorial. Unfortunately they never finished or took out the links to the information for adding more themes.

Help would be wonderful, thank you in advance!

Smiling Sara

~*Sara*~

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I think this may be easier..

<?php


switch ($theme) {
    case
'theme1':
       
$stylesheet = \"theme1.css\";
       
$headerimg = \"<div id=\"logo\">Image for theme one goes here</div>\";
        break;
    case 'theme2':
       
$stylesheet = \"theme2.css\";
       
$headerimg = \"<div id=\"logo\">Image for theme 2 goes here</div>\"; 
        break;
    case 'theme3':
       
$stylesheet = \"theme3.css\";
       
$headerimg = \"<div id=\"logo\"><img src=\"theme3.gif\" alt=\"logo\" /></div>\";
        break;
    default:
       
$stylesheet = \"theme3.css\";
       
$headerimg = \"<div id=\"logo\"><img src=\"theme3.gif\" alt=\"logo\" /></div>\";
        break;
}


?>
Just add more cases before the default line.. If you wanted to add a theme called 'blueberry', you'd add these lines:
<?php
case 'blueberry':
   
$stylesheet = \"blueberry.css\";
   
$headerimg = \"<div id=\"logo\"><img src=\"blueberry.gif\" alt=\"logo\" /></div>\";
    break;
?>

Mark Hensler
If there is no answer on Google, then there is no question.

DC_Sara's picture

She has: 392 posts

Joined: Jan 2002

Thanks Mark, I will try this! Smiling

Sara

DC_Sara's picture

She has: 392 posts

Joined: Jan 2002

I just tested it and it's wonderful. THANK YOU MARK! Smiling I have been pounding my head on the wall for a while with this hurdle. Now, I have learned something as well.

You can see the test blog here: http://www.blogland.com/b2/

Smiling Sara

~*Sara*~

He has: 1,016 posts

Joined: May 2002

Just to make it a little easier so that you don't have to add more code for every single theme...

<?php
//set the default theme
if(!isset($theme)) {
 
$theme = \"theme3\";
}
//whatever
$theme is, that CSS and logo are loaded.
$stylesheet = $theme . \".css\";
$headerimg = \"<div id=\\"logo\\"><img src=\\"\" . $theme . \".gif\\" alt=\\"logo\\" /></div>\";
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

and then I add "?theme=not_a_them" to the URL...

He has: 1,016 posts

Joined: May 2002

Quote: Originally posted by Mark Hensler
and then I add "?theme=not_a_them" to the URL...

Well, if a user wants to edit the URL just to mess with it then they know what they're doing, right? But you could add a little check that will make sure the format is "theme#" where # is a number. Again, this code is just to make it easier so you can add themes without having to edit the code every time but if it's very important that the users can't edit the URL to a theme that doesn't exist then switch() is the way to go.

<?php
//set the default theme
if(!preg_match(\"/^theme\\d+$/\", $theme)) {
 
$theme = \"theme3\";
}
?>

And Mark, I'm not trying to say your code is bad or anything like that at all, in fact it is very good but I thought for a user that doesn't know much about programming and how to edit/add to it, this way might be better (easier)...

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Your probably right. I was just, eh... testing you. Wink

DC_Sara's picture

She has: 392 posts

Joined: Jan 2002

Thanks for that Zollet although simplier to you, it's more confusing to me. I guess I have to stare at things a while! Smiling I will look at it this afternoon and break it apart.

I tried to take out the escapes on the above code, that I did use, and received a parse error, I used single quotes as well. Any clues as to why that happened?

Also, this is in my index.php to call in the themes.php

<!-- The theme links --><h2>Skin me</h2><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme1">Fall</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme2">America</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme3">Butterflies*</a><br />
<A href="<?= $_SERVER['PHP_SELF']; ?>?theme=theme4">Halloween</a><br /><br />
'

Is there a way to make it where I can put the themes in separate folders. The above code makes it impossible and I don't know how to edit it to make it work. I have everything in the blog directory and I hate that as it's hard to keep track of everything.

Thanks for y'all's help. Smiling

Sara

~*Sara*~

He has: 1,016 posts

Joined: May 2002

You could make a directory for each of your themes.. and then instead of loading theme1.css, theme2.css, you could load ./theme1/style.css, ./theme2/style.css, etc...

Simple change...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.