函数文档

register_sidebars()

💡 云策文档标注

概述

register_sidebars() 函数用于快速创建多个侧边栏,适用于主题开发或内部使用。它基于 register_sidebar() 构建,支持自定义参数,如未提供名称或ID,函数会自动生成。

关键要点

  • 用于批量注册侧边栏,简化开发流程
  • 参数 $number 指定创建数量,默认值为 1
  • 参数 $args 可传递数组或字符串,定义侧边栏属性,如 id 和 name
  • id 参数作为唯一标识符基础,多侧边栏时自动追加数字后缀
  • name 参数支持占位符 %d,用于多侧边栏时的动态命名
  • 函数内部处理冲突ID,确保唯一性

代码示例

// 注册一个名为 Sidebar 的侧边栏
register_sidebars();

// 创建两个名为 "Foobar 1" 和 "Foobar 2" 的侧边栏
register_sidebars( 2, array( 'name' => 'Foobar %d' ) );

// 创建两个侧边栏,标题用 h2 标签包裹
register_sidebars( 2, array(
	'before_title' => '<h2>',
	'after_title'  => '</h2>'
) );

📄 原文内容

Creates multiple sidebars.

Description

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.

See also

Parameters

$numberintoptional
Number of sidebars to create.

Default:1

$argsarray|stringoptional
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.
  • name string
    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’.

Default:array()

Source

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 );
	}
}

Changelog

Version Description
2.2.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Examples
    This will register 1 sidebar named Sidebar:

    register_sidebars();

    This will create 2 sidebars named “Foobar 1″ and “Foobar 2″:

    register_sidebars( 2, array( 'name' => 'Foobar %d' ) );

    This will create 2 sidebars with the title wrapped in

    and

    register_sidebars( 2, array(
    	'before_title' => '<h1>',
    	'after_title'  => '</h1>'
    ) );