Subject: Changing the theme with the topic

Posted on: 29/08/04 04:43am
By: Dirk

For a site I'm working on, I wanted the theme to change when you change the topic. Now, this has been discussed before and the Chameleon hack was developed as a result.

However, when all you need are a few color changes, there's a simpler solution that doesn't require any changes to Geeklog's core code.

This relies on the fact that you can use PHP in a theme's header.thtml file. To keep things readable, PHP code can also be added to the functions.php file that's part of every theme, so that you could add a function to that file and then simply call it from header.thtml.

So here's a simplified version of what I did:

In the theme's functions.php, add a function like this:
function theme_emitCSS ()
{
    global $topic;

    switch ($topic) {
        case 'GeekLog':
            $color = 'red';
            break;

        case 'General':
            $color = 'green';
            break;

        default:
            $color = 'black';
            break;
    }

    $retval = '<style type="text/css">' . LB
            . '.current-color {' . LB
            . '  color: ' . $color . ';' . LB
            . '  background-color: white;' . LB
            . '}' . LB
            . '</style>' . LB;

    return $retval;

}
This function simply checks what the current topic is and then emits CSS for a class called 'current-color', setting the foreground color accordingly.

All you have to do now is to add a call to that function in your theme's header.thtml like this:
<head>
...
<?php theme_emitCSS (); ?>
</head>
and then use class="current-color" wherever you need it, e.g. in the blockheader.thtml template file.

This can be extended to emit CSS using background images and other, more complicated CSS rules. You can also check what the current PHP file is (using $HTTP_SERVER_VARS['PHP_SELF'], after adding $HTTP_SERVER_VARS to the list of global variables for that function) to select colors for Geeklog's calendar, links section, etc.

Hope it helps someone ...

bye, Dirk

Changing the theme with the topic

Posted on: 08/11/05 04:45pm
By: orfilms

It doesn't seem to be working for me, i put the php call in the header, and in functions I'm putting:

function theme_emitCSS ()
{
    global $topic;

    switch ($topic) {
        case 'DVD':
            $color = 'slashdvd.jpg';
            break;

        default:
            $color = 'logo/imagerotator.php';
            break;
    }

    $retval = '<style type="text/css">' . LB
            . '#header {' . LB
            . 'width: 100%;' . LB
            . 'height: 150px;' . LB
            . 'white-space: nowrap;' . LB
            . 'background-color: #897F75;' . LB
            . 'text-align: left;' . LB
            . 'background-image: url(images/$color);' . LB
            . 'background-repeat: no-repeat;' . LB
            . 'padding: 0px 0px 0px 0px;' . LB
            . '}' . LB
            . '</style>' . LB;

    return $retval;

}


Shouldn't that work? Nothing comes up in the page source where i put the php call...

Changing the theme with the topic

Posted on: 08/11/05 05:00pm
By: Dirk

That CSS snippet should be there, at least, if you do a "view source" (or whatever the option is called) in your browser, i.e. have a look at the actual HTML of the page.

What won't work is the use of $color, as you put it between single quotes there. PHP doesn't replace variables enclosed in single quotes, so you would end up with '$color' in your CSS.

bye, Dirk

Changing the theme with the topic

Posted on: 08/11/05 05:09pm
By: orfilms

Well nothing is showing up in the source at all where i put the php call:

<?php theme_emitCSS (); ?>


I'm assuming there must be something wrong with my call? Is there any way to put the php in the header.thtml to see if that works?

Changing the theme with the topic

Posted on: 08/11/05 05:18pm
By: Dirk

Whoops, the above example is missing a "print" statement:
<head>
...
<?php print theme_emitCSS (); ?>
</head>

Funny that nobody noticed (or bothered to mention) it in over a year ...

bye, Dirk

Changing the theme with the topic

Posted on: 09/11/05 02:51am
By: orfilms

This worked great...

but than I think it caused a few errors (I'm pretty sure it was this mod)

When people try to log off or log on it doesn't let them and gives them a refresh page that says:

"Warning: Cannot modify header information - headers already sent by (output started at /home/virtual/site128/fst/var/www/html/layout/No_Advertisements/functions.php:6 in /path/www/html/users.php on line 647

Warning: Cannot modify header information - headers already sent by (output started at /home/virtual/site128/fst/var/www/html/layout/No_Advertisements/functions.php:6 in /path/www/html/users.php on line 649"

Any idea what could be causing this and how it could be fixed? I'm taking the mod off the site until I can figure it out...

Changing the theme with the topic

Posted on: 10/11/05 01:38pm
By: orfilms

Could anyone tell me how to fix this error?

Changing the theme with the topic

Posted on: 30/11/05 04:37pm
By: Chalkhillian

You should do a search for 'headers already sent' - you will get a direct hit. Chances are that when you updated your php file, either you or your editor of choice placed some white space in the php file where it cannot be tolerated.

Changing the theme with the topic

Posted on: 07/06/06 05:47pm
By: beewee

I want to use this nice trick, and just want to load a different CSS for each topic, would this work?

function style_emitCSS ()
{
    global $topic;

    switch ($topic) {
        case 'topic1':
            $style = 'style1.css';
            break;
			
			case 'topic2':
            $style = 'style2.css';
            break;

        default:
            $style = 'style.css';
            break;
    }

    $retval = '<link rel="stylesheet" type="text/css" href="/' . LB
               . '$style;' . LB
            . '"/>' . LB;

    return $retval;

}


and in the header this:

<?php print style_emitCSS (); ?>


I'm no developer, so I'm simply adapting excisting code without exactly knowing what I'm doing...

Changing the theme with the topic

Posted on: 08/06/06 10:16am
By: Remdotc

it would be better if the theme code was set per topic , or per page , as defined by admin

you could also define this on the topic out link in lib-common.php
you could also edit the index.php and add a case statement
or for better compatablity and easier upgrades you could add the code to functions.php in your theme, then call the function in the header template


Changing the theme with the topic

Posted on: 08/06/06 05:02pm
By: beewee

[QUOTE BY= Remdotc]for better compatablity and easier upgrades you could add the code to functions.php in your theme, then call the function in the header template
[/QUOTE]

I believe that this thread started with this, so I already meant this.

Changing the theme with the topic

Posted on: 17/06/06 07:20pm
By: stevelucky

this code changes the color based on topic. but what about static pages? i have a bunch of static pages and still want the header to change (images though, instead of colors). any ideas?

Changing the theme with the topic

Posted on: 18/06/06 03:25am
By: Dirk

[QUOTE BY= stevelucky] this code changes the color based on topic. but what about static pages?[/QUOTE]
Same principle: Find out where on the site you are and emit CSS or HTML accordingly.

The PHP variable $_SERVER['PHP_SELF'], for example, contains .../staticpages/index.php when a static page is displayed.

bye, Dirk

Geeklog - Forum
https://www.geeklog.net/forum/viewtopic.php?showtopic=39655