1. 首页
  2. WordPress 函数手册

register_sidebars()

register_sidebars( int $number = 1, array|string $args = array() )

创建多个边栏。
Creates multiple sidebars.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

另见函数 register_sidebar()


参数(Parameters)

参数 类型 必填 说明
$number (int) 可选
Number of sidebars to create. $args 可选
(Optional) Array or string of arguments for building a sidebar. ‘id’(string) 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. 可选

返回(Return)

无返回值


源码(Source)

/**
 * 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);
	}
}
更新版本 源码位置 使用 被使用
2.2.0 wp-includes/widgets.php:166 0 3

笔记(Notes)

实例

register_sidebars() 为WP2原创文章,链接:https://www.wp2.cn/functions/register_sidebars/

发表评论

您的电子邮箱地址不会被公开。