Skip to content

Instantly share code, notes, and snippets.

@caseydriscoll
Last active November 5, 2015 22:41
Show Gist options
  • Save caseydriscoll/88844c8a42887f4e3f27 to your computer and use it in GitHub Desktop.
Save caseydriscoll/88844c8a42887f4e3f27 to your computer and use it in GitHub Desktop.
Body Class Explanation
// Edit 0 - 2015-11-05 16:55:34 - Init
// Edit 1 - 2015-11-05 17:04:18 - Add global and short circuit
// Edit 2 - 2015-11-05 17:17:44 - Add is_tree
// Edit 3 - 2015-11-05 17:39:14 - Remove is_tree, use get_post_ancestors
// Hook into the 'body_class' filter with the name of a unique function
add_filter( 'body_class', 'page_parent_specific_body_classes' );
// Callback function for adding a class to the body element
//
// $classes is an array of classes that will be added to the 'body' element
//
function page_parent_specific_body_classes( $classes ) {
// The current page being made on the server
// By calling 'global' we move if from the $GLOBALS variable
// and make it more easily accessible here.
//
// http://php.net/manual/en/language.variables.scope.php
//
// WordPress stores the current post object in the $GLOBALS array
//
global $post;
// Assign values to variables so we can use them later
$about_page_id = 11;
$event_page_id = 13;
// Get a list of all the parent pages above this page
// https://codex.wordpress.org/Function_Reference/get_post_ancestors
$ancestors = get_post_ancestors( $post->ID );
// The 'root' parent page will be the last page in the list
// This would be the 'about' page or the 'events' page even if there are a lot of parents
//
// This root variable will be a -1 if there are no parent pages or the index of the last item
$root = count( $ancestors ) - 1;
// If the $root variable is a number bigger than 0 (not -1)
// it means there is a parent in the ancestors array,
// so just grab that id in the ancestors array and make that the $root variable
// If the $root variable is less than 0,
// it means the count above was empty (0 then subtract 1)
// and that means this page has no parents
// so it is it's own root and make $root equal to the current page id
//
// This syntax (?:) is known as the 'ternary operarator'
// http://php.net/ternary#language.operators.comparison.ternary
//
// Basically, if the condition before the '?' ($root > 0) is true,
// then do the first thing right after the '?' ($root = $ancestors[ $root ] )
// if the condition is false, to the thing after the ':' ($root = $post->ID)
$root = $root > 0 ? $ancestors[ $root ] : $post->ID;
// We only want to add a class to the about and events page (id is 11 or 13)
// If we are currently on another page, we can just leave
// This is known as 'short circuiting'
//
// http://programmers.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement
//
// You need to return the $classes array as it is though, as this is a filter
if ( $root != $about_page_id && $root != $event_page_id ) {
return $classes;
}
// Now we know the value of our 'root' page, which should only be 11 or 13
// Add the appropriate body class
if ( $root == $about_page_id ) {
$classes[] = 'about-tree';
}
if ( $root == $event_page_id ) {
$classes[] = 'events-tree';
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment