treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress Sub Category "Tree" Problem

  • 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:


    <div id=\"imageBox\">
    <?php if (is_category('school-news-and-events')) { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-headmaster-thomas-beazley.jpg\" alt=\"Headmaster Thomas A. Beazley\" />
    <?php } elseif (is_category('preschool')) { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-preschool-jennifer-vest.jpg\" alt=\"Jennifer Vest Head of Preschool at Miss Lee's\" />
    <?php } elseif (is_category('lower-school')) { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-lower-andy-surber.jpg\" alt=\"Andy Surber Head of Lower School\" />
    <?php } elseif (is_category('middle-school')) { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-middle-thor-kvande.jpg\" alt=\"Thor Kvande Head of Middle School\" />
    <?php } elseif (is_category('after-school-care')) { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-aftercare-diann-youngblood.jpg\" alt=\"Diann Youngblood Director of After School and Enrichment\" />
    <?php } else { ?>
    <img src=\"<?php bloginfo('template_url'); ?>/images/sidebar-top-feature-home.jpg\" alt=\"Grace St. Lukes\" />
    <p id=\"specialP\">(The above image shows if on any page that doesnt have an<br />assigned category. Ex: Archives, search &amp; Home)</p>
    <?php } ?>
    </div>
  • any ideas guys?
  • Moved to CMS section, might get more looks here.
  • 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.
    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:
    $category = get_the_category();
    if (isset($category[0]))
    echo $category[0]->category_description;


    Hope that helps!