Widget Area: Modify $param[‘xy’] from widget-$n

The dynamic_sidebar_params filter is called before each widget is displayed, so multiple times in the same sidebar. It filters the $args and $instance parameters that will be passed to the widget.

If you only want to execute it for the third widget of a specific sidebar, you should check for the sidebar ID (found in the id value of the $args array, which is passed first), and then count the widgets until you arrive at n. There are different ways to do this: you can get all widgets of the current sidebar and compare the ID of the n-th with the one you are filtering now, or you can just count it in your filter, resetting the count when you get a new sidebar name. Or you could replace dynamic_sidebar() in your template with a call to a function of your own that will first reset the widget count and then start filtering.

This is an example of the second type: counting widgets in the function, resetting the count each time we start a new sidebar:

add_filter( 'dynamic_sidebar_params', 'wpse15024_dynamic_sidebar_params' );
function wpse15024_dynamic_sidebar_params( $params )
{
    // These are the targets. Modify them here:
    $targeted_sidebar="sidebar-bottom";
    $targeted_widget = (int) 3;

    static $widget_counter = array();
    static $current_sidebar = null;

    $args = $params[0];
    $instance = $params[1];

    if ( $current_sidebar != $args['id'] ) {
        $current_sidebar = $args['id'];
        $widget_counter[$current_sidebar] = 0;
    }

    if ( $current_sidebar == $targeted_sidebar && $widget_counter[$current_sidebar] == $targeted_widget ) {
        $args['before_widget'] = '<div id="'.@$args['widget_id'].'" class="span-6 last">';
    }

    $widget_counter[$current_sidebar]++;

    return array( $args, $instance );
}

Leave a Comment

tech