0

How can I add special classes ( like " japan japan_index" ) for body class in category-japan.php and for category.php something like this " archive archive_index " and also I'm using this code to put categories name on single.php and index.php body class

    // Adding categories name to body class
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_nicename;
    }
  }
    else{
        $classes[] = 'index';
    }
  // return the $classes array
  return $classes;
}

// Allowed classes
add_filter( 'body_class', 'remove_some', 10, 2 );
function remove_some( $wp_classes, $extra_classes ) {

    // List of the only WP generated classes allowed
    $whitelist = array( 'index', 'japanese', 'japan', 'interviews', 'travel', 'reviews', 'series', 'archive' );

    // Filter the body classes
    $wp_classes = array_intersect( $wp_classes, $whitelist );

    // Add the extra classes back untouched
    return array_merge( $wp_classes, (array) $extra_classes );
}

1 Answer 1

0

If i understood what you are lookin for, this should work, it uses get_queried_object() function:

// Adding categories name to body class on archives
add_filter('body_class','add_category_to_body_class');
function add_category_to_body_class( $classes ) {
    // check if in category
    if ( is_category() ) {
        // get the queried object, in this case, a category object, and assigns to a variable
        // see https://codex.wordpress.org/Function_Reference/get_queried_object
        $category_object = get_queried_object();
        // get the slug
        $classes[] = $category_object->slug;
        $classes[] = $category_object->slug . '_index';
    }
  // return the $classes array
  return $classes;
}

but you can mix it to your single function:

// Adding categories name to body class on archives
add_filter('body_class','add_category_to_body_class');
function add_category_to_body_class( $classes ) {
    if ( is_single() ) {
        global $post;
        // assigns post it to variable to avoid reading an array for every loop iteration
        // maybe a micro optmization? Guilty as charged!
        $post_id = $post->ID;
        foreach( ( get_the_category( $post_id ) ) as $category ) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
            $classes[] = $category->category_nicename . '_index';
        }
    }
    // check if in category
    else if ( is_category() ) {
        // get the queried object, in this case, a category object, and assigns to a variable
        // see https://codex.wordpress.org/Function_Reference/get_queried_object
        $category_object = get_queried_object();
        // get the slug
        $classes[] = $category_object->slug;
        $classes[] = $category_object->slug . '_index';
    }
    else {
        $classes[] = 'index';
    }
    // return the $classes array
    return $classes;
}

I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.