In some cases you want to generate css on the fly. I wanted to generate a random background image for each post for instance. You can do that with a dynamic stylesheet. It’s pretty straight foreward: Basically you just change the .css extension into .php . and offcourse change the style reference in the head of the page as well.
I have red that you have to let the browser know that this is a css doc though cause it might think otherwise (I have had it working wiothout though I think. You do that as follows:

Add this to the start of the dynamic style.php

[DIV1] <? php header(“content-type: text/css”); ?> [/DIV1] (mind the space). Now in my style.php I have written the following:
[DIV1]  background-image: url(http://www.mydomain.com/images/random/random/<? php $randnr= rand (1,10); echo $randnr;?>.png); [/DIV1]
(read HERE about the random image snippet)
This way the background-image url is generated dynamically in the style.php

The way to anchor this stylesheet in the wp-header file is as follows:
in your themes functions.php add this:

[DIV1]  wp_enqueue_style(‘mystyles ‘, ‘/wp-content/themes/twentyten/style.php’, array(), ’1.0′, ‘screen’);  [/DIV1] 

I don’t know about the exact array options, but this is how it works for me.
www.michielvandijk.nl

Tags: , , , , , ,