Title: PHP Variables &#038; CSS
Last modified: August 19, 2016

---

# PHP Variables & CSS

 *  [shedwannits](https://wordpress.org/support/users/shedwannits/)
 * (@shedwannits)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/php-variables-038-css/)
 * Hello,
 * i would like posts to have different repeating backgrounds depending on what 
   category they belong to.
 * All posts from every category appear on one page, with the most recent post appearing
   first. Each post uses the same `div id`, so I can’t define the `background` property
   as anything in CSS – that’s what should change depending on the category of the
   post.
 * So I did some research into using PHP and CSS to create variables – I got as 
   far as using `style.php` instead of `style.css` and that worked fine.
    I understand
   how to declare variables in PHP but something just doesn’t work.
 * What I would like to happen:
 * Category backgrounds are uploaded to `img` folder eg `art_bg.jpg`
 * CSS `background` property of div in `style.php` is a variable:
    `background: 
   url(img/<?= $category; ?>_bg.jpg);`
 * Variable is declared in `style.php` (in the same place where the browser is instructed
   to interpret the code as CSS):
    `$category = (don't know!);`
 * This is the stage where I am stuck! If the value isn’t in quotes, the stylesheet
   breaks. Template Tags can’t be used outside The Loop, but I need the Template
   Tag `the_category` to appear in the variable declaration for my idea to work –
   even if they did, I don’t think they would be recognised in quotes!
 * Any ideas? Can my CSS/PHP plan be followed through? Would appreciate any help,
   this is doing my head in rather! 🙂
 * Thanks,
 * James 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Thread Starter [shedwannits](https://wordpress.org/support/users/shedwannits/)
 * (@shedwannits)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1010978)
 * somehow managed to overlook googling:
 * “different background each category wordpress”
 * which returned a page that suggests that each category have its own div, and 
   conditions be included in The Loop to state which div should be used for each
   category.
 * think this might be the answer to my problem but still trying it out.
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1010980)
 * Do this….
 * `<link rel="stylesheet" href="yourpath/style.php?cat=<?php echo get_the_category();?
   >" type="text/css" media="screen" />`
 * Appending a URL to the end, then in style.php… do the checks…
 *     ```
       <?php
       switch($_GET['cat']) {
         case '1':
         ?>
         .someclass {some css stuff}
         <?php
         break;
         case '2':
         ?>
         .someotherclass {some css stuff}
         <?php
         break;
         case '3':
         ?>
         .someotherclassagain {some css stuff}
         <?php
         break;
       } ?>
       ```
   
 * Or if you prefer the if else way….
 *     ```
       <?php
       if($_GET['cat'] == '1') { ?>
       .catclass {css stuff}
       <?php
       } elseif($_GET['cat'] == '2') { ?>
       .catclass {css stuff}
       <?php
       } ?>
       ```
   
 * Hope that helps..
 *  Thread Starter [shedwannits](https://wordpress.org/support/users/shedwannits/)
 * (@shedwannits)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1011037)
 * hi t31ios thanks very much for your speedy reply
 * I decided to go for the if statements within the loop and have separate divs 
   for categories but thanks again for your help, it’s good to know it can be done.
 * james
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1011043)
 * You could also do this where you call the stylesheet…
 *     ```
       <link rel="stylesheet" href="yourpath/style.php?cat=<?php if(is_category('somecat')) { echo 'somecat';}
       elseif(is_category('anothercat')) { echo 'anothercat';} // and so on...
       ?>" type="text/css" media="screen" />
       ```
   
 * That way you get use of the WP functions…
 * And again just check in style.php using if else or switch/case, whichever you
   prefer..
 * This being an alternative to using get_the_category(), which i think actually
   outputs linked categories, not just the cat name…
 *  [Gilligan](https://wordpress.org/support/users/playgod/)
 * (@playgod)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1011222)
 * I find it cleaner and simpler to have a separate CSS for each category via a 
   functions.php in the themes directory.
    e.g.
 *     ```
       function cat1style_css() {
       if (is_category('1')) {?>
       <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/cat1.css" /> <?php }
       }
       add_action('wp_head', 'cat1style_css');
   
       function cat2style_css() {
       if (is_category('2')) {?>
       <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/cat2.css" /> <?php }
       }
       add_action('wp_head', 'cat2style_css');
       ```
   
 * With this technique you can achieve a true cascade if you order the category 
   functions properly. You may name the stylesheets and functions whatever you wish,
   for even more simplicity. (is_category variables should be numeric according 
   to the cat#.)
 * ref: themeshaper.com
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1011223)
 * You could wrap that all into 1 function, but it really depends on your usage 
   of the function(s) i guess.
 * You could also check if the file exists before including it.. again this depends
   what you want…

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘PHP Variables & CSS’ is closed to new replies.

## Tags

 * [css](https://wordpress.org/support/topic-tag/css/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [Stylesheet](https://wordpress.org/support/topic-tag/stylesheet/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 6 replies
 * 3 participants
 * Last reply from: [t31os](https://wordpress.org/support/users/t31os/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/php-variables-038-css/#post-1011223)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
