• hello, how add whole category into body class? the code i got from google is

    
    // add parent category class
    add_filter('body_class','body_class_section');
    function body_class_section($classes) {
        global $wpdb, $post;
        if (is_page()) {
            if ($post->post_parent) {
                $parent  = end(get_post_ancestors($current_page_id));
            } else {
                $parent = $post->ID;
            }
            $post_data = get_post($parent, ARRAY_A);
            $classes[] = 'parent-' . $post_data['post_name'];
        }
        return $classes;
    }
    

    But it only return last child category. i’d like to return from parent,child, child of child and so on category. how do i achieve this?

    thank you

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You only are getting the last because you are using end(get_post_ancestors()). Remove the end() to get all ancestors. You will get an array of ancestor IDs. Unless you are OK with classes such as parent-456, you’ll need to loop through the results and get each related post object from which the post_name can be extracted.

Viewing 1 replies (of 1 total)
  • The topic ‘Add Parent and childs category to body class’ is closed to new replies.