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;
}
`
- WordPress Security – Why Updates and Security Measures are Crucial? - 19 March 2023
- WordCamp Poland 2023 Gliwice – WordPress user conference - 28 February 2023
- How do I restore a previous version of WordPress when an update has failed? - 9 January 2023
Leave a Reply
Want to join the discussion?Feel free to contribute!