Skip to content

Instantly share code, notes, and snippets.

@theodorocaliari
Last active August 29, 2015 14:10
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 theodorocaliari/6fc936e94f75e3cca8e2 to your computer and use it in GitHub Desktop.
Save theodorocaliari/6fc936e94f75e3cca8e2 to your computer and use it in GitHub Desktop.
<?php
/**
* Count number of widgets in a sidebar
* Used to add classes to widget areas so widgets can be displayed one, two, three or four per row
*/
function slbd_count_widgets( $sidebar_id ) {
// If loading from front page, consult $_wp_sidebars_widgets rather than options
// to see if wp_convert_widget_settings() has made manipulations in memory.
global $_wp_sidebars_widgets;
if ( empty( $_wp_sidebars_widgets ) ) :
$_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
endif;
$sidebars_widgets_count = $_wp_sidebars_widgets;
if ( isset( $sidebars_widgets_count[ $sidebar_id ] ) ) :
$widget_count = count( $sidebars_widgets_count[ $sidebar_id ] );
$widget_classes = 'widget-count-' . count( $sidebars_widgets_count[ $sidebar_id ] );
if ( $widget_count % 4 == 0 || $widget_count > 6 ) :
// Four widgets er row if there are exactly four or more than six
$widget_classes .= ' per-row-4';
elseif ( $widget_count >= 3 ) :
// Three widgets per row if there's three or more widgets
$widget_classes .= ' per-row-3';
elseif ( 2 == $widget_count ) :
// Otherwise show two widgets per row
$widget_classes .= ' per-row-2';
endif;
return $widget_classes;
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment