Jameyson Mac Donald
Jameyson Mac Donald

Reputation: 23

How To Add a Class To a Custom Sidebar in WordPress

I just learned how to create a custom WordPress theme. I would like to assign a class to my sidebar for styling, but I have not found anything online that explains how to do this. The code I added to my functions.php file to register my sidebar is:

    if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'id'            => 'sidebar-1',
        'description'   => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
        'before_widget' => '',
        'after_widget'  => '',
        'before_title'  => '',
        'after_title'   => '',
    ));

And the code I added to my sidebar.php file is:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<?php endif; ?>

Any help would be greatly appreciated!

Upvotes: 2

Views: 1786

Answers (2)

Prakash Singh
Prakash Singh

Reputation: 651

register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'twentysixteen' ),
        'id'            => 'sidebar-1',
        'description'   => 'Add widgets here to appear in your sidebar.',
        'before_widget' => '<aside id="%1$s" class="widget custom-class %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h2 class="widget-title custom-class">',
        'after_title'   => '</h2>',
    ) );

Try this in functions.php file

Upvotes: 1

topdown
topdown

Reputation: 456

Use before_widget and after_widget to add a wrapper.

Upvotes: 1

Related Questions