Title: CSS based on category
Last modified: August 19, 2016

---

# CSS based on category

 *  Resolved [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/)
 * Hi-
 * I am trying to work through an issue and need some help.
 * I have my homepage that requires me to exclude some categories in the listing.
   See this code:
 *     ```
       <?php
          if (is_home()) {
             query_posts("cat=-17, -7, -6, -18");
          }
       ?>
   
       <?php if (have_posts()) : ?>
       	<?php while (have_posts()) : the_post(); ?>
       ```
   
 * Additionally, I have a specific category that needs different css styling from
   the other posts. But, because I utilize the links that filter posts ([http://address.com/hihf/?tag=new](http://address.com/hihf/?tag=new)),
   I can NOT add another query like this:
 *     ```
       <?php $my_query = new WP_Query('cat=6'); ?>
   
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
       ```
   
 * The reason is it keeps the new query posts when I visit the filtered links: [http://address.com/hihf/?tag=new](http://address.com/hihf/?tag=new).
 * **How can I have two differently styled posts dependent on the category all within
   the same main loop?**

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

 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303926)
 * Any ideas, I can’t seem to combine these without getting a php error, is it even
   possible to combine these?
 *  [xdesi](https://wordpress.org/support/users/xdesi/)
 * (@xdesi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303930)
 * Not too sure if I understand your problem right, but if you wanted to apply a
   specific style if a post within the loop was within a certain cat you could do
   something like:
 *     ```
       <?php if(in_category('5')){ ?>
       <div style="special-style">post content</div>
       <?php } else { ?>
       <div style="normal-style">post content</div>
       <?php } ?>
       ```
   
 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303932)
 * Oh, ok, let me try that.
 * Is there a way to always have a certain categories appear either on the top or
   bottom? I want all the other posts to appear first then cat 6 to appear last.
 *  [xdesi](https://wordpress.org/support/users/xdesi/)
 * (@xdesi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303940)
 * One way would to be loop through the posts excluding cat 6, and then loop through
   them again just showing cat 6.
 *     ```
       <?php query_posts('cat=-6'); ?>
       <?php while (have_posts()) : the_post(); ?>
       //Show post stuff
       <?php endwhile;?>
   
       <?php query_posts('cat=6'); ?>
       <?php while (have_posts()) : the_post(); ?>
       //Show post stuff
       <?php endwhile;?>
       ```
   
 * That’s untested but think it should work, not sure if you might have to have 
   rewind_posts() in between the 2 loops.
    More of a reference on [multiple loops](http://codex.wordpress.org/The_Loop#Multiple_Loops)
   here.
 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303941)
 * Thats actually what I did. But when I visit those links that filter by tag, for
   some reason the second loop doesn’t get factored into that filter…
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303943)
 * If you’re using `query_posts`, remember to use `<?php wp_reset_query();?>` after
   the first Loop has finished and before you issue the second query_post.
 * [http://codex.wordpress.org/Function_Reference/wp_reset_query](http://codex.wordpress.org/Function_Reference/wp_reset_query)
 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303944)
 * still didn’t work, the posts in the second query stay on every page even if they
   don’t match the tag/cat or whatever
 *  [xdesi](https://wordpress.org/support/users/xdesi/)
 * (@xdesi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303945)
 * Ah I knew there was something like a reset in-between!!
 * > still didn’t work, the posts in the second query stay on every page even if
   > they don’t match the tag/cat or whatever
 * Not too sure what you mean here? Are the posts displaying as..
 * All posts from every cat apart from 6
 * All posts just from cat 6
 * What added functionality did you want in addition to this if it is working?
 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303953)
 * When I go to: [http://address.com/hihf/?tag=whatever](http://address.com/hihf/?tag=whatever)
   I want the posts in the second query to be considered in this. Right now only
   the first query is looked at.
 * Does this make sense? I know it hard to explain
 *  [xdesi](https://wordpress.org/support/users/xdesi/)
 * (@xdesi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303955)
 * What template page is this archive.php or index.php ??
 * How about:
 *     ```
       <?php
       if(!is_tag()){
       query_posts('cat=-6');
       }?>
       <?php while (have_posts()) : the_post(); ?>
       //Show post stuff
       <?php endwhile;?>
   
       <?php
       if(!is_tag()){
       <?php wp_reset_query();?>
       query_posts('cat=6');
       <?php while (have_posts()) : the_post(); ?>
       //Show post stuff
       <?php endwhile;?>
       }?>
       ```
   
 * If not you can create a tag.php template file to override and just have a normal
   loop
 *  Thread Starter [jdestree](https://wordpress.org/support/users/jdestree/)
 * (@jdestree)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303959)
 * And we have a winner! Thank you!!

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

The topic ‘CSS based on category’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 3 participants
 * Last reply from: [jdestree](https://wordpress.org/support/users/jdestree/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/css-based-on-category/#post-1303959)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
