Sidebars allow you display widgets inside your theme. In this post, we will teach you the way to create sidebar and display that sidebar in your theme.
First thing you need to do is register the sidebar or widget area. So copy the below code and paste in your functions.php file. Once you registered the sidebar, It will be displayed in your Appearance => Widget screen in the backend.(WP admin).
function register_sidebar_widget() {
register_sidebar( array(
'name' =>__( 'Sidebar', 'wpb'),
'id' => 'sidebar',
'description' => __( 'Appears on the static front page template', 'wpb' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'register_sidebar_widget' );
Now You need to copy and paste the below code in your template files to display the sidebar.
<?php if ( is_active_sidebar( 'sidebar' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar' ); ?>
</div>
<?php endif; ?>
In this example codes, we have registered and displayed the widget are in template files.
Please let me know, If you have any queries or feedback.