我已将侧栏分配到functions.php
:
register_sidebar(
array (
\'name\' => \'Footer Widgets Area\',
\'id\' => \'footer_widgets_area\',
\'description\' => \'Assign Maximum 3 widgets into this footer sidebar area.\',
\'class\' => \'my-class\',
\'before_widget\' => \'<li id="%1$s" class="widget-container %2$s">\',
\'after_widget\' => "</li>",
\'before_title\' => \'<h3 class="widget-title">\',
\'after_title\' => \'</h3>\'
)
);
在页脚中,我想在激活的小部件数量之后将特定的CSS类加载到小部件容器中。所以我用了这个:
$our_sidebar = wp_get_sidebars_widgets();
$widget_counter = count( $our_sidebar[\'footer_widgets_area\'] );
if( $widget_counter == 1 ) {
$the_class = \' widen-full\';
} elseif( $widget_counter == 2 ){
$the_class = \' widen-duo\';
} else {
$the_class = \' widen-trio\';
}
我的页脚现在正在加载的内容是:
<div class="widget-area" id="footer-widget-area">
<ul class="xoxo">
<li class="widget-container widget_archive" id="archives-2">
<h3 class="widget-title">Archives</h3>
Archive Widget Content
</li>
<li class="widget-container widget_calendar" id="calendar-2">
Calendar Widget
</li>
<li class="widget-container widget_meta" id="meta-3">
Meta Widget
</li>
</ul>
</div>
请注意,如果我可以
echo
我的班级
$the_class
进入
<li>
然后我的问题就解决了。
<li class="widget-container widget_archive" id="archives-2">
虽然我经常使用WordPress,但我害怕挂钩和过滤器,在大多数情况下,我对它们有一点了解。如果有人不仅能给我提供解决方案,而且能解释与此相关的过程,那将是我更好的成就。那就是我未来的成就。
PS: 不知何故,中定义的类register_sidebar()
没有回声。我不知道为什么。
最合适的回答,由SO网友:Subharanjan 整理而成
function widget_display_callback( $params ) {
global $wp_registered_widgets;
global $my_widget_num; // Global a counter array
$id = $params[0][\'widget_id\'];
$sidebar_id = $params[0][\'id\'];
/* Set some count for each widgets */
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;
}
$widget_counter = $my_widget_num[ $sidebar_id ];
if( $widget_counter == 1 ) {
$the_class = \' widen-full \';
} elseif( $widget_counter == 2 ){
$the_class = \' widen-duo \';
} else {
$the_class = \' widen-trio \';
}
if ( !empty( $sidebar_id ) && $sidebar_id == \'footer_widgets_area\' && !empty( $the_class ) ) {
// add your classes
$classe_to_add = \' \' . $the_class . \' \'; // make sure you leave a space at the end
$classe_to_add = \'class=" \' . $classe_to_add;
$params[0][\'before_widget\'] = str_replace( \'class="\', $classe_to_add, $params[0][\'before_widget\'] );
}
return $params;
}
add_filter( \'dynamic_sidebar_params\', \'widget_display_callback\', 10 );
基本上,您需要使用“dynamic\\u sidebar\\u params”钩子动态地将新类添加到每个小部件中。我已经实现了上面相同的功能,以在侧栏中查找小部件计数和小部件索引(对于您的情况,是:footer\\u widgets\\u area)。尝试在活动主题功能上添加这些代码。php文件,并检查这是否适合您!!
您必须替换此行中的提要栏id
if ( !empty( $sidebar_id ) && $sidebar_id == \'footer_widgets_area\' && !empty( $the_class ) ) {