Support » Fixing WordPress » Change Header Display Based on Top Level Category

  • Goal: To change header images and alter nav-menu colors based on top level category of a Post or a Category archive page.

    Requirements: Child cats (whether Post or Cat archive) should display the same as their top-level cats. Only needs to work for specified top cats; others are ignored (Posts have multiple hierarchies). Must work for a single Post or a Category archive page.

    Solution: Add a body CSS class based on specified top-level cats (and their children); otherwise revert to a default class. Then let CCS do the rest (w/ class-specific background urls and color declarations.)

    Question: The following works, but wondering if the code/tactic is sound?

    The top-cats I’m trapping for have slugs of 'fire', 'energy', and 'land', also trolling for any of their descendants.

    Code: In <head> of header.php:

    // Add body class based on top-level category, or default to:
    $cathead = 'cathead_home';
    // Is Post (or Category archive) in 'X' category or a descendant of 'X'?
    // Uses post_is_in_descendant_category() in functions.php; requires cat ID#.
    // For readability using cat slug instead, and getting ID via get_term_by().
    // To print, insert this PHP in HTML body tag: body_class($cathead);
    if (is_category('fire') || in_category('fire') || post_is_in_descendant_category(get_term_by('slug','fire','category'))) {
    	$cathead = 'cathead_fire';
    } elseif (is_category('energy') || in_category('energy') || post_is_in_descendant_category(get_term_by('slug','energy','category'))) {
    	$cathead = 'cathead_energy';
    } elseif (is_category('land') || in_category('land') || post_is_in_descendant_category(get_term_by('slug','land','category'))) {
    	$cathead = 'cathead_land';

    Above requires this function in functions.php:
    post_is_in_a_descendant_category() (from WP codex).

    And this <body> tag in header.php:
    <body <?php body_class($cathead); ?>>

    As mentioned, it all works well. Just wondering if anyone sees improvements or mistakes? Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Check out my Body Class Enhanced plugin. It does exactly what you are trying to do, but in a much cleaner way.

    ambrosite, thanks for the suggestion of your plug. looked at your code and seems like it’ll do a fine job of adding body classes for Pages and single Posts.

    but i also need it to work for Category archives, both for top-level cats and children of the those cats. and while your plug does find parents, i’m not sure it finds grandparents, aka, cats that are 2+ levels down from top, which is something else i need.

    your plug also adds a extra wp_query that i’m not sure i need as the cat info is already available w/o a query.

    You’re right, my plugin does not find grandparent categories, however I’ll look into adding that feature. Depending on what’s involved, I could have a new version released as early as tomorrow evening.

    All right, I’m working on the category archives now, but here’s what I have done so far for single posts. Suppose you have a category hierarchy that looks like this:

    Animals -> Canines -> Dogs -> Beagles
    Comics -> Peanuts

    If you create a single post titled “Snoopy” and assign it to both the “Beagles” and “Peanuts” category, then the body classes output by my plugin look like this:

    postname-snoopy single-beagles parent-dogs parent-canines parent-animals single-peanuts parent-comics

    That gives you what you need for single posts, right?

    I’ve got the category archives working now. In my example above, if you view the archive page for the “Beagles” category, these are the classes on the body:

    archive category category-beagles parent-category-dogs parent-category-canines parent-category-animals

    Here’s the download link. Please let me know if this works for you.

    Ambrosite: Your plugin’s a thing of beauty. Does all I need, and opens up other possibilities that may be needed later. Scrapping my code, using your plug, and will resolve this topic.

    Just to clarify, this CSS are the body classes of any archive or post in category (or child of category) with slug mycat:

    .category-mycat, .parent-category-mycat, .parent-mycat, .single-mycat { // "mycat" styles }

    Above would style them all the same. Respectively, the classes are for mycat Category archive, any of mycat‘s children Category archives, a single Post in category mycat, and a single Post in any of mycat‘s child categories. The first class listed is WP body_class() inserted, the other 3 are from your plug.

    Also we could give a Page w/ slug mycat the same styles by adding , .pagename-mycat to above list of classes.

    Very slick. Much thanks.

    Yes, your explanation is exactly correct, that is how I intended the plugin to be used.

    Thanks for your feedback. I’ll get the documentation cleaned up and submit the updated version to the WP plugin repository today. If you think of any more features that might be useful, let me know.

    The above 1.2 download link is dead. I have placed the new version in the WordPress plugin directory, it can be downloaded there:

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Change Header Display Based on Top Level Category’ is closed to new replies.