Skip to content

Instantly share code, notes, and snippets.

Last active May 24, 2020
What would you like to do?
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:
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
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