How to create/use WordPress sidebar?

We should know first what sidebar is in WordPress. A WordPress page is divided into header, sidebar, content, and footer.



A WordPress page is displayed according to the page template. In the page template, call get_header() to display the header area, call get_sidebar() to display the sidebar area, and call get_footer to display the footer area. If you want a no sidebar article page, you need not look for a theme without sidebar, you just need to comment/delete the line calling get_sidebar() in your current theme file.

get_sidebar(name) loads the sidebar template named sidebar_name.php, so you can specify how to display a sidebar (such as specify sidebar widget spacing) in sidebar template(s) in your theme directory and you can customize different sidebar templates.

However, the concept of sidebar is not just for page layout. More importantly, sidebar is used as container of WordPress Widgets. You can drag widgets into a sidebar in admin panel. From the view point of programming, a sidebar is identified by a sidebar-id. To create a wordpress sidebar to be used to accept widgets in admin panel or displayed on the webpage, you need (and only need) to register the sidebar first by calling

register_sidebar( array(
'name'          => __( 'Primary Sidebar', 'twentyfourteen' ),
'id'            => 'sidebar-1',
'description'   => __( 'Main sidebar that appears on the left.', 'twentyfourteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget'  => '</aside>',
'before_title'  => '<h1 class="widget-title">',
'after_title'   => '</h1>',
) );

In a sidebar template, you can call dynamic_sidebar( sidebar_id) to display a sidebar or several sidebars(so whether to show sidebars and show which sidebars are determined by your theme’s page template; what to be displayed exactly for a sidebar is determined by your theme’s sidebar templates). dynamic_sidebar will call widget() function of every contained widget to display the widget itself.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply