get_sidebar( string $name = null, array $args = array() ): void|false

Loads sidebar template.

Description

Includes the sidebar template for a theme or if a name is specified then a specialized sidebar will be included.

For the parameter, if the file is called "sidebar-special.php" then specify "special".

Parameters

$namestringoptional
The name of the specialized sidebar.

Default:null

$argsarrayoptional
Additional arguments passed to the sidebar template.

Default:array()

Return

void|false Void on success, false if the template does not exist.

Source

function get_sidebar( $name = null, $args = array() ) {
	/**
	 * Fires before the sidebar template file is loaded.
	 *
	 * @since 2.2.0
	 * @since 2.8.0 The `$name` parameter was added.
	 * @since 5.5.0 The `$args` parameter was added.
	 *
	 * @param string|null $name Name of the specific sidebar file to use. Null for the default sidebar.
	 * @param array       $args Additional arguments passed to the sidebar template.
	 */
	do_action( 'get_sidebar', $name, $args );

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

	$templates[] = 'sidebar.php';

	if ( ! locate_template( $templates, true, true, $args ) ) {
		return false;
	}
}

Hooks

do_action( ‘get_sidebar’, string|null $name, array $args )

Fires before the sidebar template file is loaded.

Changelog

VersionDescription
5.5.0The $args parameter was added.
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 11 content

    Call sidebar with $args parameter (Since 5.5.0)
    Consider below is your sidebar call from anywhere inside your theme,

    <?php 
    $args = array(
    	'title' => 'Shop sidebar'
    );
    
    get_sidebar( 'shop', $args ); 
    
    ?>

    Your codes inside sidebar-shop.php file might look a like below.

    <div id="secondary" class="widget-area sidebar-shop" role="complementary">
    	<h2><?php echo esc_html( $args['title'] ); ?><h2>
    	<?php dynamic_sidebar( 'sidebar-shop' ); ?>
    </div><!-- #secondary -->
  2. Skip to note 12 content

    Conditional Statement for Any Sidebar
    In case you are making a plugin template where you do not know if there is a sidebar, for any given theme the plugin may be used with, you may check for register_sidebar function to see if any sidebar exists.

    if ( function_exists( 'register_sidebar' ) ) { 
    	get_sidebar(); 
    } 

    Or, if you know the theme registered name for the sidebar in question try:

    //for twenty-sixteen theme
        if ( is_active_sidebar( 'content-bottom' ) ) : 
            get_sidebar( 'content-bottom' ); 
        endif; 

You must log in before being able to contribute a note or feedback.