WordPress.org

Ready to get started?Download WordPress

Forums

Widgets - Adding An Additional CSS Class (2 posts)

  1. Ryan Ball
    Member
    Posted 3 years ago #

    Is there any way to add an additonal CSS class to the widget markup?

    I've registered a new sidebar via the following snippet and I'd like to insert an additional CSS class: "widget-x" (x being the order/position of the widget within the sidebar). Any suggestions?

    register_sidebar(array(
    'id' => 'footer',
    'name' => __('Footer'),
    'description' => __('Widgets in this area will be shown witin the footer.'),
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>'
    ));

  2. fadingdust
    Member
    Posted 2 years ago #

    Here ya go:

    function widget_index_class($params){
    $sw=wp_get_sidebars_widgets();
    $this_sidebar=$params[0]['id'];
    $this_widget_id=$params[0]['widget_id'];
    $widgets=array_flip($sw[$this_sidebar]);
    $this_widget_index=$widgets[$this_widget_id];
    $params[0]['before_widget'] = preg_replace( '/class="/', "class=\"widget-index{$this_widget_index} ", $params[0]['before_widget'], 1 );
    return $params;
    }
    add_filter('dynamic_sidebar_params', 'widget_index_class');

    Add the above to functions.php.. I've got it after the sidebars are registered.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.