Adding parent page slug to body classes

When you need to add  <body class=”parent-SLUGNAME”> class please add the following code to your functions.php file:

`

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;
}

`

post
Mariusz Szatkowski
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *