Need help? Check out our Support site, then


Body class all categories listed

  1. hi,
    i have an issue and have looked high and low for the answer but to no avail. i have many wordpress sites and love wordpress but on this occassion i am stumped.

    basically i have a site with many child categories of parent categories

    i.e. category 1 --> category 1.1
    category 1.2
    etc

    i want all the child categories, post pages etc to have the same parent categories sytle. i.e i everything that is in category 1 to have exactly the same backgound header etc.

    i managed this for post and pages by using the following in functons.php:

    }
    add_filter('body_class','add_category_to_single');
    function add_category_to_single($classes, $class) {
    if (is_single() ) {
    global $post;
    foreach((get_the_category($post->ID)) as $category) {
    echo $category->cat_name . ' ';
    // add category slug to the $classes array
    $classes[] = 'category-'.$category->slug;
    }
    }
    // return the $classes array
    return $classes;
    }

    this was great. for each post it showed every category that the body was in, incluuding for example 'category-1' which i could then style using CSS.

    however this was obviously not applicable for my child categories and they do not show in body class the parent category that i have assigned them.

    i would be very grateful if someone could help me to get a potential conditional format via functions.php so that for example category 1--> category 1.1 will show in body class it's parent category so it can be styled by my parent cateorgy css.
    many thanks in advance for any help. i really have spent hours searching this and to no avail for categories so it is not for the want of trying or an easy way out to ask, i am just stumped!!!!

    many thanks

    Nick

    The blog I need help with is test.keystagehistory.co.uk.

  2. The blog you specified at test.keystagehistory.co.uk does not appear to be hosted at WordPress.com.

    This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you'll find help at the WordPress.org forums.

    If you don't understand the difference between WordPress.com and WordPress.org, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It'll help people to answer your question.

    This is an automated message.

Topic Closed

This topic has been closed to new replies.

About this Topic