Created
July 5, 2015 07:40
-
-
Save hslaszlo/86640510c79cb45f8696 to your computer and use it in GitHub Desktop.
Flexible horizontal widgets
This file contains 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 | |
* source: http://slobodanmanic.com/444/flexible-horizontal-sidebars-wordpress-twenty-thirteen-child-theme/ | |
*/ | |
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; | |
} | |
?> | |
<?php | |
/* | |
* html | |
* important: the widget name is the widget id on wp admin! | |
*/ | |
?> | |
<div class="widget-area flexible-widget-area <?php echo slbd_count_widgets( 'sidebar-5' ); ?>"> | |
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('footer') ) : endif; ?> | |
</div> | |
<style type="text/css"> | |
/* Flexible widget areas - number of widgets per row (1, 2, 3 or 4) changes depending on number of widgets used */ | |
.flexible-widget-area .widget {margin: 20px 0;float: left;width: 98%;padding: 0 1%;} | |
.flexible-widget-area.widget-count-1 .widget {} | |
.flexible-widget-area.per-row-2 .widget { width: 46%;} | |
.flexible-widget-area.per-row-3 .widget {width: 31.33333%;} | |
.flexible-widget-area.per-row-4 .widget {width: 23%;} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment