WordPress at a glance

get_sidebar() WP 1.5.0

Includes the sidebar.php template file from your current theme's directory. If a $name is specified then sidebar-{name}.php will be included.

If the theme contains no sidebar.php file then the sidebar from the default theme wp-includes/theme-compat/sidebar.php will be included.

Works based on: locate_template()
Hooks from the function

Null. Nothing.


get_sidebar( $name );
The name of the specialised sidebar.
Default: null


#1 Using two sidebars in the theme

<?php get_header(); ?>
<?php get_sidebar('left'); ?>
<?php get_sidebar('right'); ?>
<?php get_footer(); ?>

#2 Including a sidebar from theme's subdirectory

// Includes 'inc/sidebar.php' file from the current theme's directory. 
// Put this code into functions.php
function theme_sidebar( $name = '' ){
	do_action( 'get_sidebar', $name );

	if( $name )
		$name = "-$name";

	locate_template("inc/sidebar$name.php", true);

// call instead of get_sidebar()


Since 1.5.0 Introduced.

Code of get sidebar: wp-includes/general-template.php WP 5.4.2

function get_sidebar( $name = null ) {
	 * Fires before the sidebar template file is loaded.
	 * @since 2.2.0
	 * @since 2.8.0 $name parameter added.
	 * @param string|null $name Name of the specific sidebar file to use. null for the default sidebar.
	do_action( 'get_sidebar', $name );

	$templates = array();
	$name      = (string) $name;
	if ( '' !== $name ) {
		$templates[] = "sidebar-{$name}.php";

	$templates[] = 'sidebar.php';

	locate_template( $templates, true );

Related Functions

From category: Theme files connection

More from Template Tags: Main Functions

No comments
        Log In . Register