有一个答案here, 但它依赖于wp_get_sidebars_widgets, 这是贬值的,也是私有的。
This code 看起来是一个更好、更强健的解决方案:
其实很简单。我用它为我的小部件添加类PureCSS 框架
在本例中,我们希望外部容器具有类pure-g
对于“网格”。以及要具有pure-u-1-*
, 其中,星号是列的总数,在本例中为小部件计数。
因此,注册侧栏:
register_sidebar([
\'name\' => __(\'Footer Widget Area\', \'pure-demo\'),
\'id\' => \'sidebar-footer\',
\'class\' => \'pure-g\',
// Next line has our callback
\'before_widget\' => \'<section class="widget %2$s \'. slbd_count_widgets( \'sidebar-footer\' ) .\'">\',
\'after_widget\' => \'</section>\',
\'before_title\' => \'<h3>\',
\'after_title\' => \'</h3>\'
]);
下面是我们的回调函数:
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_classes .= \'pure-u-1 pure-u-sm-1-1\';
$widget_classes .= \' pure-u-md-1-\' . ceil($widget_count / 2);
$widget_classes .= \' pure-u-lg-1-\' . $widget_count;
return $widget_classes;
endif;
}
然后我显示如下:
<footer class="content-info pure-g">
<?php if ( is_active_sidebar( \'sidebar-footer\' ) ) : ?>
<?php dynamic_sidebar( \'sidebar-footer\' ); ?>
<?php endif; ?>
<div id="colophon" class="site-footer" role="contentinfo">
<?php
if ( has_nav_menu( \'footer_links\' ) ) :
wp_nav_menu([\'theme_location\' => \'footer_links\', \'container_class\' => \'pure-menu\', \'menu_class\' => \'pure-menu-list\']);
endif;
?>
</div>
</footer>