Title: PHP help required!
Last modified: August 18, 2016

---

# PHP help required!

 *  [spiritbased](https://wordpress.org/support/users/spiritbased/)
 * (@spiritbased)
 * [18 years, 8 months ago](https://wordpress.org/support/topic/php-help-required/)
 * Hi there
 * I’m trying to use different class elements for each category. What I’ve got below
   works for posts categorized “music”, which have the category ID of 5. But I can’t
   figure out how to get additional ID’s to use other classes.
 * You’ll see below in bold where I tried to add another class. If I remove this
   part, it works fine.
 * Any ideas please?
 * Here’s the code…
 * <h3 class=”accordion_toggle”>
 *  <?php foreach((get_the_category()) as $cat) $id = $cat->cat_ID; ?>
    <?php if(
   $id == 5) { ?> <div class=”music”> <**?php if ($id == 3) { ?> <div class=”print”
   > <?php } else { ?><div class=”accordion_toggle”>
 *  <?php } ?>
 *  <span class=”float-left”><?php the_title(); ?></span><span class=”float-right”
   ><?php the_time(‘F jS, Y’) ?> </span>
 *  </h3>

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

 *  Thread Starter [spiritbased](https://wordpress.org/support/users/spiritbased/)
 * (@spiritbased)
 * [18 years, 8 months ago](https://wordpress.org/support/topic/php-help-required/#post-644178)
 * ttt
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 8 months ago](https://wordpress.org/support/topic/php-help-required/#post-644181)
 * The problem is that you’re missing the closing } after the first if, so since
   the first condition fails, it doesn’t bother to check the second one. You’re 
   probably getting a parse error. Instead of staring the second category as `if{`,
   start with `}elseif{`
 * But it might be easier to give each category an ID-based class name instead of
   a text-based one. That is, rather than class names like “music” and “print”, 
   use class names like “cat5” and “cat3”. Then, all you would need is this:
 *     ```
       <div class="cat<?php echo $cat->cat_ID; ?>">
       ```
   
 * Just a thought.
 *  Thread Starter [spiritbased](https://wordpress.org/support/users/spiritbased/)
 * (@spiritbased)
 * [18 years, 8 months ago](https://wordpress.org/support/topic/php-help-required/#post-644183)
 * Adam
 * Thanks man, your first suggestion has cracked it.
 * However…
 * I’m intrigued by your second. When you say “all you would need is”, could you
   tell me exactly which bit of my code I’d replace with yours?
 * I realise you didn’t have to help me in the first place, but I’d really appreciate
   you shedding some light on this for me!
 * Thanks very much
 * Gary (Spiritbased)
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/php-help-required/#post-644346)
 *     ```
       <?php foreach((get_the_category()) as $cat) $id = $cat->cat_ID; ?>
       <div class="cat<?php echo $cat->cat_ID; ?>">
       <?php } ?>
       ```
   
 * Haven’t tested it. That’s up to you. Just a suggestion for a different way to
   do it.

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

The topic ‘PHP help required!’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [customising](https://wordpress.org/support/topic-tag/customising/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * Last activity: [18 years, 7 months ago](https://wordpress.org/support/topic/php-help-required/#post-644346)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
