Skip to content

Instantly share code, notes, and snippets.

@simplethemes
Last active December 17, 2015 11:39
Show Gist options
  • Save simplethemes/5603545 to your computer and use it in GitHub Desktop.
Save simplethemes/5603545 to your computer and use it in GitHub Desktop.
Function to count Widgets in a given location and append the column classes to each.
/* Split Individual Widgets into Columns */
function st_split_widgets($sidebar_position) {
if ( is_active_sidebar( $sidebar_position )) {
// count the active widgets to determine column sizes
$the_widgets = wp_get_sidebars_widgets();
$below_content_sidebars = $the_widgets[$sidebar_position];
$number = count($below_content_sidebars);
// default
$split = "full-width";
// if only one
if ($number == "1") {
$split = "fullwidth";
dynamic_sidebar($sidebar_position);
return;
// if two, split in half
} elseif ($number == "2") {
$split = "half";
// if three, divide in thirds
} elseif ($number == "3") {
$split = "third";
// if four, split in fourths
} elseif ($number == "4") {
$split = "fourth";
}
// Turn on output buffering because we can't access individual widget objects
ob_start();
echo '<div id="aftercontent" class="'.$split.'">';
dynamic_sidebar($sidebar_position);
echo '<div class="clear"></div></div>';
$sidebar = ob_get_contents();
ob_end_clean();
// search the output for the widget-container class
$pattern = '/widget-container/';
// define replacement for columns
$replacement = 'widget-container one_'.$split;
// widget content
$content = preg_replace($pattern, $replacement, $sidebar, -1 );
// define search string
$search = "widget-container one_".$split;
// define replacement for .last class
$replace = "widget-container one_".$split." last";
// find the last instance and add the replace class
$pos = strrpos($content, $search);
if($pos !== false) {
$content = substr_replace($content, $replace, $pos, strlen($search));
}
echo $content;
} //endif
} //@end st_split_widgets()
// Assign the function to st_before_content
function st_before_content_widget() {
do_action('st_before_content_widget','above-content-widget');
}
add_action('st_before_content_widget','st_split_widgets',10,1);
add_action('st_before_content','st_before_content_widget',1);
// Assign the function to st_after_content
function st_after_content_widget() {
do_action('st_after_content_widget','below-content-widget');
}
add_action('st_after_content_widget','st_split_widgets',10,1);
add_action('st_after_content','st_after_content_widget',1);
/* Add to style.css */
#aftercontent {margin-top: 20px;}
#aftercontent.half .widget-container.S1,
#aftercontent.half .widget-container.S2,
#aftercontent.half .widget-container.S3 {width: 45.6%;}
#aftercontent.third .widget-container.S1,
#aftercontent.third .widget-container.S2,
#aftercontent.third .widget-container.S3 {width: 28.333%;}
#aftercontent.fourth .widget-container.S1,
#aftercontent.fourth .widget-container.S2,
#aftercontent.fourth .widget-container.S3 {width: 19.666%;}
@media only screen and (max-width: 959px) {
#aftercontent.half .widget-container.S1,
#aftercontent.half .widget-container.S2,
#aftercontent.half .widget-container.S3 {width: 45%;}
#aftercontent.third .widget-container.S1,
#aftercontent.third .widget-container.S2,
#aftercontent.third .widget-container.S3 {width: 27.85%;}
#aftercontent.fourth .widget-container.S1,
#aftercontent.fourth .widget-container.S2,
#aftercontent.fourth .widget-container.S3 {width: 19%;}
}
@media only screen and (max-width: 767px) {
#aftercontent.half .widget-container,
#aftercontent.third .widget-container,
#aftercontent.fourth .widget-container { width: 100% !important;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment