Grow your CSS skills. Land your dream job.

WordPress Sub Category "Tree" Problem

  • # August 10, 2009 at 4:22 pm

    I’m working on a blog that uses categories for the navigation. its for a school and each part of the school has its own category, every post gets assigned the right cat and then only shows up in its section etc.

    Each category has an image in the sidebar that changes as you navigate through.

    problem is, when you go to the "Middle School" it shows the correct image in the sidebar, then when you click on a post, it shows the default image. my php isnt pulling the category despite it being in the url still. any ideas on how i can do this?

    Get the parent category of a post and display the correct image?

    here’s the code:

    Code:
    < ?php if (is_category('school-news-and-events')) { ?>
    /images/sidebar-preschool-jennifer-vest.jpg” alt=”Jennifer Vest Head of Preschool at Miss Lee’s” />
    < ?php } elseif (is_category('lower-school')) { ?>
    /images/sidebar-middle-thor-kvande.jpg” alt=”Thor Kvande Head of Middle School” />
    < ?php } elseif (is_category('after-school-care')) { ?>
    /images/sidebar-top-feature-home.jpg” alt=”Grace St. Lukes” />

    (The above image shows if on any page that doesnt have an
    assigned category. Ex: Archives, search & Home)

    < ?php } ?>

    # August 13, 2009 at 2:39 pm

    any ideas guys?

    # August 16, 2009 at 10:10 pm

    Moved to CMS section, might get more looks here.

    # November 4, 2009 at 4:38 pm

    is_category() is only true if the current page is the category’s archive page, so it won’t help you if you want to style a single post page.

    To determine if the current post has a certain category assigned you can use in_category(). E.g.

    Code:
    if (in_category(‘middle-school’)) : /* do stuff */ endif;

    A simpler solution might involve putting what you want to appear in the side bar in the category’s description and simply using the following in your single.php:

    Code:
    $category = get_the_category();
    if (isset($category[0]))
    echo $category[0]->category_description;

    Hope that helps!

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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".