I have assigned a Sidebar into 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>'
)
);
In footer I want to load specific CSS class into the widget container after the amount of widgets activated. So I used this:
$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';
}
What my footer is loading now is:
<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>
Please note that if I can echo
my class $the_class
into the <li>
then my problem is solved.
<li class="widget-container widget_archive" id="archives-2">
Though I work a lot with WordPress, but I'm afraid of hooks and filters, and in most of the cases I understand them a little. It'd be my better achievement, if somebody can NOT just provide me a solution, but EXPLAIN the process associated with this. Then it'd be my future achievement.
PS: Somehow, the class defined in register_sidebar()
is not echoing. I don't know why.
id
of the widget area is mismatched? You have registered it as'id' => 'footer_widgets_area',
and the html isid="footer-widget-area"
.register_sidebar()
correctly, it's nothing to connect both of them here. the'id' => 'footer_widgets_area',
is to call thedynamic_sidebar('footer_widgets_area')
, and theid="footer-widget-area"
is hard-coded.