1

So I want to add a widget area in my sidebar, that resizes the widgets depending on how many widgets are active. So like, if there are 3 active widgets, then each one will be like 33% the width of the container.

My widget is set up like :

register_sidebar(array(
    'name' => __( 'Front page Widgets' ),
    'id' => 'front-page',
    'description' => __( 'Widgets in this area will appear on the front page.' ),
    'before_widget' =>'<li class="span4">',
    'before_title' => '<h3>',
    'after_title' => '</h3>'
));

I can count the number of active widgets with this

$sidebars = wp_get_sidebars_widgets();
$footerWidgetCount =  count( $sidebars['footer-widgets'] );

But how would I go about passing $footerWidgetCount into my widget? Id like to be able to pass a new value for before_widget so I could resize it on the fly. Is that possible?

1

2 Answers 2

1

If I'm understanding correctly:

$data = array(
  'name' => __( 'Front page Widgets' ),
  'id' => 'front-page',
  'description' => __( 'Widgets in this area will appear on the front page.' ),
  'before_widget' =>'<li class="span4">',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
);
$data['before_widget'] = "<li class='span$footerWidgetCount'>";
register_sidebar($data);
Sign up to request clarification or add additional context in comments.

Comments

0

A bit messy, I will try to clean this up later, but how we are doing it in HoverCraft:

function hovercraft_callout() {
    
$callout_menu = wp_nav_menu( array( 
    'theme_location' => 'cta-sidebar-callout', 
    'menu_class' => 'cta', 
    'container_class' => 'cta-sidebar-callout',
     'echo' => false,
    ) );
    
$after_widget_callout = ' '. $callout_menu . '</div>';
    
    $callout_array = array(
        'name'          => 'Callout',
        'id'            => 'hovercraft_callout',
        'before_widget' => '<div class="widget-callout widget-wrapper">',
        'after_widget'  => $after_widget_callout,
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
        );

register_sidebar($callout_array);
}

add_action( 'widgets_init', 'hovercraft_callout' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.