From d8c00eff2aa095101c78dfb5e28bcd1f5c428a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Ko=CC=88nemann?= Date: Tue, 17 Jan 2017 15:31:12 +0100 Subject: [PATCH] Dynamize footer full and static hero widget area Now adds the proper Bootstrap class to a widget, depending on the number of widgets. For example col-md-6 if two widgets are there, col-md-4 if three published etc. --- inc/widgets.php | 54 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/inc/widgets.php b/inc/widgets.php index d9649dfd..79a5ea05 100644 --- a/inc/widgets.php +++ b/inc/widgets.php @@ -4,6 +4,43 @@ * * @package understrap */ +/** + * 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 .= ' col-md-3'; + elseif ( 6 == $widget_count ) : + // If two widgets are published + $widget_classes .= ' col-md-2'; + elseif ( $widget_count >= 3 ) : + // Three widgets per row if there's three or more widgets + $widget_classes .= ' col-md-4'; + elseif ( 2 == $widget_count ) : + // If two widgets are published + $widget_classes .= ' col-md-6'; + elseif ( 1 == $widget_count ) : + // If just on widget is active + $widget_classes .= ' col-md-12'; + endif; + return $widget_classes; + endif; +} + if ( ! function_exists( 'understrap_widgets_init' ) ) { /** @@ -44,22 +81,23 @@ if ( ! function_exists( 'understrap_widgets_init' ) ) { 'name' => __( 'Hero Static', 'understrap' ), 'id' => 'statichero', 'description' => 'Static Hero widget. no slider functionallity', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', + 'before_widget' => '
', + 'after_widget' => '
', + 'before_title' => '

', + 'after_title' => '

', ) ); register_sidebar( array( 'name' => __( 'Footer Full', 'understrap' ), 'id' => 'footerfull', 'description' => 'Widget area below main content and above footer', - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', + 'before_widget' => '', + 'before_title' => '

', + 'after_title' => '

', ) ); } } // endif function_exists( 'understrap_widgets_init' ). add_action( 'widgets_init', 'understrap_widgets_init' ); +