Last active
April 3, 2018 03:32
-
-
Save webdevsuperfast/0865946fdc59bd807a88c02540164798 to your computer and use it in GitHub Desktop.
Get number of widgets in a sidebar and add first and last widget class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function cur_target_sidebar_add_classes_to_params($params) { | |
| global $my_widget_num; // Global a counter array | |
| $sidebar_id = $params[0]['id']; | |
| if ( $sidebar_id == 'sidebar' ) { | |
| $registered_widgets = wp_get_sidebars_widgets(); | |
| if(!isset($registered_widgets[$sidebar_id]) || !is_array($registered_widgets[$sidebar_id])) { // Check if the current sidebar has no widgets | |
| return $params; // No widgets in this sidebar... bail early. | |
| } | |
| $number_of_widgets = count($registered_widgets[$sidebar_id]); | |
| $rounded_number_of_widgets = floor(12 / $number_of_widgets); //Rounds number of widgets down to a whole number | |
| if(!$my_widget_num) {// If the counter array doesn't exist, create it | |
| $my_widget_num = array(); | |
| } | |
| if(isset($my_widget_num[$sidebar_id])) { // See if the counter array has an entry for this sidebar | |
| $my_widget_num[$sidebar_id] ++; | |
| } else { // If not, create it starting with 1 | |
| $my_widget_num[$sidebar_id] = 1; | |
| } | |
| $classes = 'span' . $rounded_number_of_widgets; | |
| if($my_widget_num[$sidebar_id] == 1) { // If this is the first widget | |
| $classes .= ' first-widget '; | |
| } elseif($my_widget_num[$sidebar_id] == count($registered_widgets[$sidebar_id])) { // If this is the last widget | |
| $classes .= ' last-widget '; | |
| } | |
| $params[0]['before_widget'] = preg_replace('/class=\"/', 'class="' . $classes . ' ', $params[0]['before_widget'], 1); | |
| } | |
| return $params; | |
| } | |
| add_filter('dynamic_sidebar_params','cur_target_sidebar_add_classes_to_params'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| ?> | |
| register_sidebar( array( | |
| 'name' => 'Footer', | |
| 'id' => 'footer', | |
| 'description' => 'Footer Items', | |
| 'before_widget' => '<div id="%1$s" class="footer-widget %2$s '. slbd_count_widgets( 'footer' ) .'">', | |
| 'after_widget' => '</div><!-- .footer-widget -->', | |
| 'before_title' => '<h3>', | |
| 'after_title' => '</h3>', | |
| ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment