Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Stylesheet issue

header ('content-type:text/css') strips variables?

         

doodlebee

7:50 pm on May 7, 2007 (gmt 0)

10+ Year Member



I was helping someone with a site, and we were trying to get a dynamic stylesheet to work. now, I've done them before, and they work well, but usually, I place my variables in the top, before the "stylesheet" area comes in, so that the code can pull from the already-set list of items.

However, the person I was helping needed the variables to also be dynamic. They were using a CMS system, and needed the variables to be based on article titles.

So, for example, they had an article titled "spoons". The dynamic CSS would use the article title to set the background image for that article, so it would do:

body {background-image:url('../images/spoons.gif');

What I did was place a piece of code in the dynamic stylesheet so that it would see whatever page it was on, and use that page name/article title and use it as the name of the image to be pulled in.

The code looked liek so:

<?php

header('Content-type: text/css');

global $post;
$dpname = $post->post_name;
?>

body { background-image:url("../images/<?php echo $dpname;?>_bg.jpg"); }

(For the record, she's using WordPress, and "$post_>post_name" is what pulls the title of the article from the database and displays it as a variable)

IN any other PHP page, I can use that code, and it will spit out *exactly* what I need. However, in this file, it will not. $dpname always returns as empty.

I'm *suspecting* it's because of the "header('Content-type: text/css');" line - that this is stripping everything down to text-only. But that seems odd to me, because if I hard-code the variables in there, it pulls it through just fine.

Would anyone happen to know *why* the variables are getting stripped? Even better - know how to fix it?

leadegroot

3:31 am on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you pulled in the wordpress include line at the top of the file to get that data and started The Loop?

doodlebee

1:33 pm on May 8, 2007 (gmt 0)

10+ Year Member



Oh my God! I feel like an idiot.

That *so* worked. Thank you!

(sometimes you need someone to stab you in the eye with the obvious! Thank you!)

leadegroot

8:59 am on May 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:) Its easy to forget that the css file is actually a different page load.