register_sidebar

函数
register_sidebar ( $args = array() )
参数
  • (array|string) $args { Optional. Array or string of arguments for the sidebar being registered. @type string $name The name or title of the sidebar displayed in the Widgets interface. Default 'Sidebar $instance'. @type string $id The unique identifier by which the sidebar will be called. Default 'sidebar-$instance'. @type string $description Description of the sidebar, displayed in the Widgets interface. Default empty string. @type string $class Extra CSS class to assign to the sidebar in the Widgets interface. Default empty. @type string $before_widget HTML content to prepend to each widget's HTML output when assigned to this sidebar. Receives the widget's ID attribute as `%1$s` and class name as `%2$s`. Default is an opening list item element. @type string $after_widget HTML content to append to each widget's HTML output when assigned to this sidebar. Default is a closing list item element. @type string $before_title HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element. @type string $after_title HTML content to append to the sidebar title when displayed. Default is a closing h2 element. @type string $before_sidebar HTML content to prepend to the sidebar when displayed. Receives the `$id` argument as `%1$s` and `$class` as `%2$s`. Outputs after the {@see 'dynamic_sidebar_before'} action. Default empty string. @type string $after_sidebar HTML content to append to the sidebar when displayed. Outputs before the {@see 'dynamic_sidebar_after'} action. Default empty string. @type bool $show_in_rest Whether to show this sidebar publicly in the REST API. Defaults to only showing the sidebar to administrator users. }
    Required:
    Default: array()
返回值
  • (string) Sidebar ID added to $wp_registered_sidebars global.
定义位置
相关方法
register_sidebarsunregister_sidebaris_registered_sidebarregister_sidebar_widgetunregister_sidebar_widget
引入
2.2.0
弃用
-

register_sidebar: 这个函数用来在WordPress中注册一个新的侧边栏。侧边栏是用来在WordPress网站上显示小工具的,比如最近的文章列表或搜索栏。

为一个单独的侧边栏建立定义并返回ID。

接受一个字符串或一个数组,然后根据新侧边栏的一组默认参数来解析它。如果不包括这些参数,WordPress会根据当前注册的侧边栏的数量自动生成一个侧边栏的ID和名称。

当允许自动生成名称和ID参数时,请记住,你的侧边栏的增量可以随着时间的推移而改变,这取决于安装了哪些其他插件和主题。

如果在调用这个函数时,还没有添加对”小工具”的主题支持,它将通过使用add_theme_support()自动启用。

function register_sidebar( $args = array() ) {
	global $wp_registered_sidebars;

	$i = count( $wp_registered_sidebars ) + 1;

	$id_is_empty = empty( $args['id'] );

	$defaults = array(
		/* translators: %d: Sidebar number. */
		'name'           => sprintf( __( 'Sidebar %d' ), $i ),
		'id'             => "sidebar-$i",
		'description'    => '',
		'class'          => '',
		'before_widget'  => '<li id="%1$s" class="widget %2$s">',
		'after_widget'   => "</li>n",
		'before_title'   => '<h2 class="widgettitle">',
		'after_title'    => "</h2>n",
		'before_sidebar' => '',
		'after_sidebar'  => '',
		'show_in_rest'   => false,
	);

	/**
	 * Filters the sidebar default arguments.
	 *
	 * @since 5.3.0
	 *
	 * @see register_sidebar()
	 *
	 * @param array $defaults The default sidebar arguments.
	 */
	$sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) );

	if ( $id_is_empty ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */
				__( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ),
				'<code>id</code>',
				$sidebar['name'],
				$sidebar['id']
			),
			'4.2.0'
		);
	}

	$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar;

	add_theme_support( 'widgets' );

	/**
	 * Fires once a sidebar has been registered.
	 *
	 * @since 3.0.0
	 *
	 * @param array $sidebar Parsed arguments for the registered sidebar.
	 */
	do_action( 'register_sidebar', $sidebar );

	return $sidebar['id'];
}

常见问题

FAQs
查看更多 >