Skip to content

Instantly share code, notes, and snippets.

@mdingemanse
Last active October 29, 2021 16:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdingemanse/7ccbb92e4cb22db86da422897adcaa3a to your computer and use it in GitHub Desktop.
Save mdingemanse/7ccbb92e4cb22db86da422897adcaa3a to your computer and use it in GitHub Desktop.
Wordpress Multisite add site id and page slug to body class
// Add site-id to body class to allow site-specific styling in a central style.css file
// (this code goes in your theme's functions.php or in a site-specific plugin)
function site_id_in_body_class($classes) {
$this_id = get_current_blog_id();
$classes[] = 'site-id-'.$this_id;
return $classes;
}
add_filter('body_class', 'site_id_in_body_class');
// Add page slug to body class to allow page-specific styling
// based on: http://timneill.net/2013/05/wordpress-add-page-slug-to-body-class-including-parents/
function page_slug_in_body_class($classes) {
// You can modify this check so it will run on every post type
if (is_page()) {
global $post;
// If we *do* have an ancestors list, process it
// http://codex.wordpress.org/Function_Reference/get_post_ancestors
if ($parents = get_post_ancestors($post->ID)) {
foreach ((array)$parents as $parent) {
// As the array contains IDs only, we need to get each page
if ($page = get_page($parent)) {
// Add the current ancestor to the body class array
$classes[] = "{$page->post_type}-{$page->post_name}";
}
}
}
// Add the current page to our body class array
$classes[] = "{$post->post_type}-{$post->post_name}";
}
return $classes;
}
add_filter('body_class', 'page_slug_in_body_class');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment