Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress][functions] Add body classes for active menu locations and active widget areas. In reply to this post (🇩🇪): http://torstenlandsiedel.de/2016/05/01/css-klasse-hinzufuegen-fuer-mobilgeraete/
<?php
/**
* Alters body class for populated menu locations.
* @param array $classes Registered body classes
* @return array Altered body classes
*/
function ch20160502__body_class_active_menus( $classes ) {
$menu_locations = get_nav_menu_locations();
if ( empty( $menu_locations ) )
return $classes;
foreach ( $menu_locations as $location => $items_count ) {
if ( $items_count > 0 )
$classes[] = sprintf( 'menu-%s-active', esc_attr( $location ) );
}
return $classes;
}
add_filter( 'body_class', 'ch20160502__body_class_active_menus' );
<?php
/**
* Alters body class for active sidebars.
* @param array $classes Registered body classes
* @return array Altered body classes
*/
function ch20160502__body_class_active_sidebars( $classes ) {
foreach ( $GLOBALS['wp_registered_sidebars'] as $id => $args ) {
if ( is_active_sidebar( $id ) )
$classes[] = sprintf( 'widget-area-%s-active', esc_attr( $id ) );
}
return $classes;
}
add_filter( 'body_class', 'ch20160502__body_class_active_sidebars' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment