register_sidebars

声明

<?php register_sidebars( $number, $args ); ?>

建立多个侧边栏(工具条)。

注册一个或多个侧边栏在当前主题中使用。多数主题只有一个侧边栏。由于以上原因,参数number是可选择的并且默认值是1。

args数组可以包含一系列侧边栏的名字,如果有多于一个的侧边栏。如果没有定义名字,默认使用’Sidebar’。

参数

$number
(int) (必需的) 要建立的侧边栏的数量。

$args
(string/array) (必需的) 建立基于名字和’id’的边栏。

例子

下面这个例子将会建立一个侧边栏,名为Sidebar:
register_sidebars();

下面这个例子将会建立两个侧边栏,名为“Foobar 1″和“Foobar 2″:
register_sidebars(2, array(‘name’=>’Foobar %d’));

变更日志

Since: 2.2.0

源文件

register_sidebars()位于wp-includes/widgets.php。

/**
* 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.
*
* The default for the name is “Sidebar #”, with ‘#’ being replaced with the
* number the sidebar is currently when greater than one. If first sidebar, the
* name will be just “Sidebar”. The default for id is “sidebar-” followed by the
* number the sidebar creation is currently at.
*
* @since 2.2.0
*
* @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here.
* @uses parse_str() Converts a string to an array to be used in the rest of the function.
* @uses register_sidebar() Sends single sidebar information [name, id] to this
*    function to handle building the sidebar.
*
* @param int $number Number of sidebars to create.
* @param string|array $args Builds Sidebar based off of ‘name’ and ‘id’ values.
*/
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’);
}

if (isset($args['id'])) {
$_args['id'] = $args['id'];
} else {
$n = count($wp_registered_sidebars);
do {
$n++;
$_args['id'] = “sidebar-$n”;
} while (isset($wp_registered_sidebars[$_args['id']]));
}

register_sidebar($_args);
}
}

register_sidebar

可用来注册一个边栏。

相关文章:让wordpress模板支持侧边栏插件Sidebar Widgets的方法

Leave a Reply