函数原型
register_sidebars( int $number = 1, array|string $args = array() )
函数描述
Creates multiple sidebars.
是否弃用
未弃用
函数参数
-
$number
int
Optional - Number of sidebars to create.
Default:
1 -
$args
array|string
Optional - Array or string of arguments for building a sidebar.
idstringThe base string of the unique identifier for each sidebar. If provided, and multiple sidebars are being defined, the ID will have "-2" appended, and so on.
Default'sidebar-'followed by the number the sidebar creation is currently at.namestringThe name or title for the sidebars displayed in the admin dashboard. If registering more than one sidebar, include'%d'in the string as a placeholder for the uniquely assigned number for each sidebar.
Default'Sidebar'for the first sidebar, otherwise ‘Sidebar %d’.
Default:
array()
函数返回值
无
函数位置
File: wp-includes/widgets.php.
函数源码
function register_sidebars( $number = 1, $args = array() ) {
global $wp_registered_sidebars;
$number = (int) $number;
if ( is_string( $args ) ) {
parse_str( $args, $args );
}
for ( $i = 1; $i <= $number; $i++ ) {
$_args = $args;
if ( $number > 1 ) {
if ( isset( $args['name'] ) ) {
$_args['name'] = sprintf( $args['name'], $i );
} else {
/* translators: %d: Sidebar number. */
$_args['name'] = sprintf( __( 'Sidebar %d' ), $i );
}
} else {
$_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' );
}
// Custom specified ID's are suffixed if they exist already.
// Automatically generated sidebar names need to be suffixed regardless starting at -0.
if ( isset( $args['id'] ) ) {
$_args['id'] = $args['id'];
$n = 2; // Start at -2 for conflicting custom IDs.
while ( is_registered_sidebar( $_args['id'] ) ) {
$_args['id'] = $args['id'] . '-' . $n++;
}
} else {
$n = count( $wp_registered_sidebars );
do {
$_args['id'] = 'sidebar-' . ++$n;
} while ( is_registered_sidebar( $_args['id'] ) );
}
register_sidebar( $_args );
}
}
源码链接
变更日志
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |

