Body class all categories listed

  • Author
  • #1165240


    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

    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:

    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


    The blog I need help with is



    The blog you specified at does not appear to be hosted at

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

    If you don’t understand the difference between and, 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.

The topic ‘Body class all categories listed’ is closed to new replies.