Title: Problem styling based on category
Last modified: August 20, 2016

---

# Problem styling based on category

 *  Resolved [MagnusCreed](https://wordpress.org/support/users/magnuscreed/)
 * (@magnuscreed)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/)
 * I am trying to style an h2 based upon what category the post is in.
 * I have made this code and placed it within the loop and all works well…for the
   first of each ‘cat_name==’. As soon as a 2nd/3rd/etc post is categorized as ‘
   Branding’, those subsequent posts dont get styled at all.
 *     ```
       <?php
            foreach((get_the_category()) as $category) {
            if ($category->cat_name == 'Branding') {
            echo the_date('m/d', '<h2 class="postDateBrnd postBar">', '</h2>'); }
            elseif ($category->cat_name == 'Consulting') {
            echo the_date('m/d', '<h2 class="postDateCnslt postBar">', '</h2>'); }
            else {
       }
       ?>
       ```
   

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205878)
 * Does your theme use the body_class() function on its opening `<body>` element?
 *  Thread Starter [MagnusCreed](https://wordpress.org/support/users/magnuscreed/)
 * (@magnuscreed)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205881)
 * It does not. I am just learing to make wordpress themes and this would be the
   first I have heard of this function.
 * Should it be in there?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205882)
 * Yes. See [http://codex.wordpress.org/Function_Reference/body_class](http://codex.wordpress.org/Function_Reference/body_class)
 *  Thread Starter [MagnusCreed](https://wordpress.org/support/users/magnuscreed/)
 * (@magnuscreed)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205917)
 * Ok, just want to make sure I am understanding. The body_class() function is a
   better way of doing what i tried to do above and isnt a tag on to what is above?
 * Also, i want to make sure that I am clear on what I am doing. I am trying to 
   display the headings for each category differently but within the same page. 
   ie. in my blog roll, i am listing the category and trying to style each category
   heading with a different background image.
 * I am just clarifying as what i am reading seems to be more related to styling
   differently for a category when that category might be on its own page or be 
   a single page view. One of the examples is:
 * > To add a category class to single post pageviews and template files
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205918)
 * > The body_class() function is a better way of doing what i tried to do above
 * Correct. You may also want to ensure that your theme uses the [post_class() function](http://codex.wordpress.org/Function_Reference/post_class).
 *  Thread Starter [MagnusCreed](https://wordpress.org/support/users/magnuscreed/)
 * (@magnuscreed)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205975)
 * Thank you, your tip was enlightening.

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

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

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [elseif](https://wordpress.org/support/topic-tag/elseif/)
 * [if](https://wordpress.org/support/topic-tag/if/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [MagnusCreed](https://wordpress.org/support/users/magnuscreed/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/problem-styling-based-on-category/#post-3205975)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
