Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Last active May 18, 2023 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackpyle/006fb22e081422d162c0bf3ef6cd2cd8 to your computer and use it in GitHub Desktop.
Save zackpyle/006fb22e081422d162c0bf3ef6cd2cd8 to your computer and use it in GitHub Desktop.
Page Slug Body Class #wordpress
<?php // ignore - for gist formatting only
// Add Page Slug to Body Class
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );
<?php // ignore - for gist formatting only
// Add Parent-Page-Slug to Child Page's Body Class
function add_parent_slug_to_child_body_class( $classes ) {
global $post;
// Check if current page has parent
if ( $post->post_parent ) {
$parent = get_post( $post->post_parent );
$classes[] = 'parent-page-' . $parent->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_parent_slug_to_child_body_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment