Home > Article > Backend Development > Explanation of PHP functions used to create and obtain sidebars in WordPress, wordpressphp_PHP tutorial
register_sidebar() (create sidebar)
Create a sidebar to place widgets. When using this function, please put it in a function and mount it to the "widgets_init" hook.
Usage
register_sidebar( $args );
Parameters
$args
(String | Array) (Optional) Parameters for the sidebar to create.
Default value:
$args = array( 'name' => __( 'Sidebar name', 'theme_text_domain' ), 'id' => 'unique-sidebar-id', 'description' => '', 'class' => '', 'before_widget' => '<li id="%1" class="widget %2">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' );
Introduction to array parameters:
Example
register_sidebar( array( 'name' => __( '右边的侧边栏' ), 'id' => 'sidebar-1', 'description' => __( '右侧边栏的小工具。' ), 'before_title' => '<h3 class="title">', 'after_title' => '</h3 class="title">', ));
Others
This function is located at: wp-includes/widgets.php
get_sidebar() (get sidebar)
get_sidebar() is used to introduce the sidebar template. If the name is specified, the sidebar-{name}.php file in the current theme root directory will be imported. If not specified, the sidebar.php file in the current theme root directory will be imported. If the file does not exist, wp-includes/theme-compat/sidebar.php will be imported. document.
Usage
get_sidebar( $name );
Parameters
$name
(String) (optional) The name of the template to import. If specified, the sidebar-{$name}.php file in the current theme root directory will be imported.
Default value: None
Example
The following code will import the sidebar.php file in the current theme root directory:
<?php get_sidebar(); ?>
The following code will import the sidebar-left.php file in the current theme root directory:
<?php get_sidebar( 'left' ); ?>
The following example introduces the left sidebar (sidebar-left.php) and the right sidebar (sidebar-right.php) respectively:
<?php get_header(); ?> <?php get_sidebar( 'left' ); ?>
Content content
<?php get_sidebar( 'right' ); ?> <?php get_footer(); ?>
Others
This function is located at: wp-includes/general-template.php