Last active
May 18, 2023 13:36
-
-
Save zackpyle/006fb22e081422d162c0bf3ef6cd2cd8 to your computer and use it in GitHub Desktop.
Page Slug Body Class #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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