Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active July 5, 2018 20:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glueckpress/05a9ebaa5f1b5e3e5926ad2a160c51b1 to your computer and use it in GitHub Desktop.
Save glueckpress/05a9ebaa5f1b5e3e5926ad2a160c51b1 to your computer and use it in GitHub Desktop.
[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