Skip to content

Instantly share code, notes, and snippets.

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 ginocremer/88865c09053d26cd6208 to your computer and use it in GitHub Desktop.
Save ginocremer/88865c09053d26cd6208 to your computer and use it in GitHub Desktop.
In vielen Fällen kann eine Klasse auf dem Body-Element nützlich sein. Zum Beispiel wenn Sie jeder einzelnen Seite eine eigene Farbgebung via CSS spendieren möchten.
function add_top_parent_body_class( $classes ) {
if(is_page()):
$top_id = '';
$ancestors = get_ancestors(get_the_ID(),'page');
if ( !empty($ancestors)):
$top_id = end($ancestors);
$classes[] = 'top-parent-'.sanitize_title(get_the_title($top_id));
else:
$classes[] = 'top-parent-'.sanitize_title(get_the_title(get_the_ID()));
endif;
else:
$classes[] = 'no-top-parent';
endif;
return $classes;
}
add_filter( 'body_class', 'add_top_parent_body_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment