• I’m trying to use a conditional to treat from one category in a list differently from those surrounding it.

    if ( in_category(22) ) {

    Using the ID works fine for this installation but I would like to use the category name instead to make it more portable.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Just put the name between the parentheses instead.

    Thread Starter eric_ie

    (@eric_ie)

    That’s actually the first thing i tried. So that line looked like
    if ( in_category('permanent') ) {

    and no luck. Do you know for a fact that works? See anything wrong with the way I’ve written it? That’s exactly what I want to do and it didn’t seem to be supported in the older version according to this Codex page. I was hoping for some more flexibility in WP 2.0

    Hmm, that should have worked. I’m stumped, because that works for is_category();. What’s the full PHP call you’re putting after the brace “{“?

    Thread Starter eric_ie

    (@eric_ie)

    Thanks for the feedback. I tried to make the code inside the braces as simple as possible to test the in_category function so it is just an echo. This code does work.

    <?php
    if ( in_category(22) ) {
    echo 'inside category 22';
    }
    ?>

    and this does not

    <?php
    if ( in_category('permanent') ) {
    echo 'inside category permanent';
    }
    ?>

    I’ve double checked the spelling and case of the category name, there doesn’t seem to be a problem there.

    According to the codex page you listed about in_category, it looks like only the ID can be used. Would is_category work for what you are attempting to do?

    Thread Starter eric_ie

    (@eric_ie)

    Thanks for your reply Jon,

    It looks like is category is used to tell when a category page is being displayed. Since I am sorting posts not displaying a category page that doesn’t seem applicable. Good to know it’s out there but I’ll have to keep looking.

    Codex reference for is_category

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘category by name’ is closed to new replies.