描述:
批量注册并创建侧边栏
用法:
<?php register_sidebars( $number, $args ); ?>
参数:
number
(integer) (可选) 要创建的侧边栏个数
默认值: 1
args
(string/array) (可选) 基于“name”和“id”值生成侧边栏。
默认值: None
- name - 侧边栏名称
- id - 侧边栏id。(注意:“%d”自动添加到第一个后提供的“id”值中;例如,“侧边栏”、“侧边栏-2”、“侧边栏-3”等)
- description -侧边栏的内容/位置的文本描述。显示在小工具管理界面上。(从2.9开始)(默认值:空)
- class - 要分配给小部件HTML的CSS类名(默认值:空).
- before_widget - HTML放在每个小部件之前。
- after_widget -HTML放在每个小部件之后。
- before_title - 在每个标题前添加的HTML。
- after_title - 在每个标题后添加的HTML。
源文件:
/**
* Creates multiple sidebars.
*
* If you wanted to quickly create multiple sidebars for a theme or internally.
* This function will allow you to do so. If you don't pass the 'name' and/or
* 'id' in `$args`, then they will be built for you.
*
* @since 2.2.0
*
* @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
*
* @global array $wp_registered_sidebars
*
* @param int $number Optional. Number of sidebars to create. Default 1.
* @param array|string $args {
* Optional. Array or string of arguments for building a sidebar.
*
* @type string $id The 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.
* @type string $name The 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'.
* }
*/
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 )
$_args['name'] = isset($args['name']) ? sprintf($args['name'], $i) : 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 ID's
while ( isset($wp_registered_sidebars[$_args['id']]) )
$_args['id'] = $args['id'] . '-' . $n++;
} else {
$n = count($wp_registered_sidebars);
do {
$_args['id'] = 'sidebar-' . ++$n;
} while ( isset($wp_registered_sidebars[$_args['id']]) );
}
register_sidebar($_args);
}
}