Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BronsonQuick/3140250 to your computer and use it in GitHub Desktop.
Save BronsonQuick/3140250 to your computer and use it in GitHub Desktop.
Count the number of widgets in a sidebar in WordPress
<?php
/**
* Count the number of widgets in a sidebar
* Works for up to ten widgets
* Usage <?php ctm_sidebar_class( 'promo' ); ?> where promo is the name of the sidebar
*/
function ctm_sidebar_class( $sidebar_name ) {
global $sidebars_widgets;
$count = count ($sidebars_widgets[$sidebar_name]);
switch ( $count ) {
case '1':
$class = 'one';
break;
case '2':
$class = 'two';
break;
case '3':
$class = 'three';
break;
case '4':
$class = 'four';
break;
case '5':
$class = 'five';
break;
case '6':
$class = 'six';
break;
case '7':
$class = 'seven';
break;
case '8':
$class = 'eight';
break;
case '9':
$class = 'nine';
break;
case '10':
$class = 'ten';
break;
default:
$class = '';
break;
}
if ( $class )
echo 'class="' . $class . '"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment