Title: get category outside the loop &#8212; code difficulty
Last modified: August 19, 2016

---

# get category outside the loop — code difficulty

 *  Resolved [csleh](https://wordpress.org/support/users/csleh/)
 * (@csleh)
 * [18 years ago](https://wordpress.org/support/topic/get-category-outside-the-loop-code-difficulty/)
 * I’m using a plugin to assign categories to pages. Works beautifully. On the page
   template I’m able to display css according to the category. There is a sidebar
   that is included and I’d like to display a certain image depending on what category
   the page or post is in.
 * This is the code that works on the page template:
 * ‘<?php if ( in_category(9) ) {
    echo ‘thing’; } elseif ( in_category(8) ) { echo‘
   thing2’; } else { echo ‘something else’; } ?>’
 * but it breaks the display when put in the sidebar.
 * Have searched and tried lots of things for getting the current category outside
   the loop but am having the devil of a time. Help greatly appreciated.

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

 *  Thread Starter [csleh](https://wordpress.org/support/users/csleh/)
 * (@csleh)
 * [18 years ago](https://wordpress.org/support/topic/get-category-outside-the-loop-code-difficulty/#post-766292)
 * I forgot I can add links in this forum. You can see what I mean here:
    [http://www.puritanautomation.com/datafiles/?page_id=14](http://www.puritanautomation.com/datafiles/?page_id=14)
 * the two left columns — the second (inner) one has a different background color
   depending on category. I’m trying to get the photo in the first one differnt 
   depending on category as well. THat info is in sidebar-left.php
 * Perhaps there is a way to query the post, get the category, then do an echo?
 *  Thread Starter [csleh](https://wordpress.org/support/users/csleh/)
 * (@csleh)
 * [17 years, 12 months ago](https://wordpress.org/support/topic/get-category-outside-the-loop-code-difficulty/#post-766459)
 * OK, I don’t need to do this — was overthinking the issue. I found some conditional
   tags that do the trick BUT if I try to use an image instead of just text, the
   site below the header comes back blank. For every page, even the ones in a different
   category.
 * So new question — how can I use an image in a conditional comment as seen below?
 *     ```
       <?php
       // let's generate info appropriate to the page being displayed
       if (in_category(9)) {
                echo "<p>we are in category automation</p>";
       } elseif (in_category(8)) {
                echo "<p>we are in category test</p>";
       } else {
              echo "<p>this is anything else</p>";
       } // That's all, folks!
       ?>
       ```
   
 * I’ve tried:
    `echo "<img src="<?php bloginfo('template_directory'); ?>/images/
   brose1.jpg" alt="test system image" />";`
 * `<img src="<?php bloginfo('template_directory'); ?>/images/brose1.jpg" alt="test
   system image" />;` (no echo)
 * `echo “<img src=”fullurlcopiedfrombrowser.jpg” />;
 * The image does display — if you visit the site you’ll see the text is on top 
   of that image. So link seems correct.
 *  Thread Starter [csleh](https://wordpress.org/support/users/csleh/)
 * (@csleh)
 * [17 years, 12 months ago](https://wordpress.org/support/topic/get-category-outside-the-loop-code-difficulty/#post-766466)
 * looking for something completely different, found this on “the undersigned” blog:
 * `<?php if ( is_single(’slug’) ) { ?>
    <img src=”” alt=”” / /> <?php } ?>’
 * works like a charm!

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

The topic ‘get category outside the loop — code difficulty’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [get category](https://wordpress.org/support/topic-tag/get-category/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [Outside The Loop](https://wordpress.org/support/topic-tag/outside-the-loop/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 1 participant
 * Last reply from: [csleh](https://wordpress.org/support/users/csleh/)
 * Last activity: [17 years, 12 months ago](https://wordpress.org/support/topic/get-category-outside-the-loop-code-difficulty/#post-766466)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
