• 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

    (@spiritbased)

    ttt

    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

    (@spiritbased)

    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)

    <?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.