Skip to content

Instantly share code, notes, and snippets.

@germanny
Created January 9, 2014 17:19
Show Gist options
  • Save germanny/8338063 to your computer and use it in GitHub Desktop.
Save germanny/8338063 to your computer and use it in GitHub Desktop.
body_class
<?php
/**
* Add new classes to the $classes array
* http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters
*/
add_filter('body_class','my_class_names');
function my_class_names($classes) {
global $post;
if ( is_front_page() ) :
$classes[] = 'home';
elseif ( is_page() ) :
$classes[] = $post->post_name;
elseif( is_archive() ) :
$classes[] = 'archive';
elseif( is_404() ) :
$classes[] = 'error';
elseif( is_search() ) :
$classes[] = 'search';
endif;
// return the $classes array
return $classes;
}
?>
<body <?php body_class(); ?>>
@germanny
Copy link
Author

/**
 * Add new classes to the $classes array
 * http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters
 */
add_filter('body_class','my_class_names');
function my_class_names($classes) {
  global $post;

  if ( is_front_page() ) :
    $classes[] = 'home';
  elseif ( is_page() ) :
    $classes[] = $post->post_name;
  elseif( is_archive() ) :
    $classes[] = 'archive';
  elseif( is_404() ) :
    $classes[] = 'error';
  elseif( is_search() ) :
    $classes[] = 'search';
  endif;

  if ( is_child('subjects') ) :
    $classes[] = 'parent-subjects';
  elseif ( is_tree('subjects') && !is_page('subjects') ) :
    $classes[] = 'grandparent-subjects';
  endif;

  if ( is_child('schools') ) :
    $classes[] = 'parent-schools';
  endif;

  // return the $classes array
  return $classes;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment