WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] body class for child categories (5 posts)

  1. njthomp
    Member
    Posted 1 year ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    after this line

    $classes[] = 'category-'.$category->slug;

    try and add:

    $parent = $category->category_parent;
    if( $parent ) {
      $parent_cat_slug = 'cat-parent-'.get_category( $parent )->slug;
      if( !in_array( $parent_cat_slug, $classes ) $classes[] = $parent_cat_slug;
    }

    the full code:

    add_filter('body_class','add_category_to_single');
    function add_category_to_single($classes) {
    if (is_single() ) {
    	global $post;
    	foreach((get_the_category($post->ID)) as $category) {
    		// add category slug to the $classes array
    		$classes[] = 'category-'.$category->slug;
    		$parent = $category->category_parent;
    		if( $parent ) {
    		  $parent_cat_slug = 'cat-parent-'.get_category( $parent )->slug;
    		  if( !in_array( $parent_cat_slug, $classes ) ) $classes[] = $parent_cat_slug;
    		}
    	}
    }
    // return the $classes array
    return $classes;
    }

    untested

  3. njthomp
    Member
    Posted 1 year ago #

    thanks very much for the reply.

    i tried this addition to the functions.php, but it did not work.

    Could this be due to the function filter still being only concerned with 'single' posts, if is single, as this is not a problem, it already works with single posts.

    i am looking for a resolution for category pages such as

    website/category-1/male/blue

    which has a list of all the blue male but is category-1 as the parent. at the moment the only body class is only showing 'blue' as the class as well as

    <body class="archive category category-blue category-182 logged-in admin-bar no-customize-support">

    i neeed this to include the parent cateogry 'category-1' so that all the child categories of /category-1/ can be styled in the same way.

    i hope this makes sense, many thanks once again for any help

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    yes - this is just for single;

    for category archive, add these new lines before

    return $classes;
    if( is_category() ) {
    $cat_id = get_query_var('cat');
    //get category parent slug if exists
    $parent_id = get_category( $cat_id )->category_parent;
    if ( $parent_id ) $classes[] = 'cat-parent-' . get_category( $parent_id )->slug;
    //get top level category ancestor slug if grandparent etc exists
    $ancestors = get_ancestors( $cat_id, 'category' );
    if( count( $ancestors ) > 1 ) $classes[] = 'cat-ancestor-' . get_category(array_pop($ancestors))->slug;
    }
  5. njthomp
    Member
    Posted 1 year ago #

    thanks a million, to get this to work i just had to change the 'cat-parent-' and 'cat-ancestor-' to 'category-' to fit with the dynamic output from body class.

    this works great now, thanks so much for your time.

    kind regards

    nick

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags