Home >Backend Development >PHP Tutorial >An explanation of the PHP functions used to create and obtain sidebars in WordPress
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 );
Arguments
$args
(String | Array) (Optional) Arguments for the sidebar to create.
Default value:
$args = array( 'name' => __( 'Sidebar name', 'theme_text_domain' ), 'id' => 'unique-sidebar-id', 'description' => '', 'class' => '', 'before_widget' => '<li>', 'after_widget' => '</li>', 'before_title' => '<h2>', 'after_title' => '</h2>' );
Array parameter introduction:
Example
register_sidebar( array( 'name' => __( '右边的侧边栏' ), 'id' => 'sidebar-1', 'description' => __( '右侧边栏的小工具。' ), 'before_title' => '<h3>', 'after_title' => '</h3>', ));
Others
The function is located at: wp-includes/widgets.php
get_sidebar() (get the 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) Introduces the name of the template. If specified, the sidebar-{$name}.php file in the current theme root directory will be introduced.
Default value: None
Example
The code below will import the sidebar.php file in the root directory of the current theme:
<?php get_sidebar(); ?>
The code below will import the sidebar-left.php file in the root directory of the current theme:
<?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 :wp-includes/general-template.php
The above is an introduction to the PHP functions used to create and obtain sidebars in WordPress, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.