Search code examples
phpwordpresswidgetwoocommercesidebar

Widget class in custom sidebar


I created a custom sidebar with WordPress and I wanted to style it, but here I encounter problem - all widgets which I add to it lose their original class and all styling, also all are wrapped in <div> which I declare in function, which is OK, but how can i make each widget keep their original classes passed by WooComerce or give each one unique class ?

Here is code:

if ( function_exists('register_sidebar') ){
    register_sidebar(array(
        'name' => 'custom_sidebar',
        'before_widget' => '<div id="shop-top">',
        'after_widget' => '</div>',
        'before_title' => '',
        'after_title' => '',
));
}

Solution

  • You've misinterpreted the arguments of register_sidebar. In order to maintain the classes of each widget you cannot hard-code each widget to have the ID of "shop-top". Try the folowing:

    $args = array(
        'name'          => __( 'Custom Sidebar', 'theme_text_domain' ),
        'id'            => 'shop-top',
        'description'   => '',
            'class'         => '',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>' );
    
    register_sidebar( $args );