register_sidebar() - WordPress 函数参考中文文档

返回介绍

register_sidebar()

发布于 2017-09-11 字数6018 浏览 877 评论 0

register_sidebar( array|string $args = array() )

Builds the definition for a single sidebar and returns the ID.


描述

Accepts either a string or an array and then parses that against a set of default arguments for the new sidebar. WordPress will automatically generate a sidebar ID and name based on the current number of registered sidebars if those arguments are not included.

When allowing for automatic generation of the name and ID parameters, keep in mind that the incrementor for your sidebar can change over time depending on what other plugins and themes are installed.

If theme support for ‘widgets’ has not yet been added when this function is called, it will be automatically enabled through the use of add_theme_support()


参数

$args

(array|string)
(Optional)
Array or string of arguments for the sidebar being registered.

  • ‘name’
    (string) The name or title of the sidebar displayed in the Widgets interface. Default ‘Sidebar $instance’.
  • ‘id’
    (string) The unique identifier by which the sidebar will be called. Default ‘sidebar-$instance’.
  • ‘描述’
    (string) 描述 of the sidebar, displayed in the Widgets interface. Default empty string.
  • ‘class’
    (string) Extra CSS class to assign to the sidebar in the Widgets interface.
  • ‘before_widget’
    (string) HTML content to prepend to each widget’s HTML output when assigned to this sidebar. Default is an opening list item element.
  • ‘after_widget’
    (string) HTML content to append to each widget’s HTML output when assigned to this sidebar. Default is a closing list item element.
  • ‘before_title’
    (string) HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element.
  • ‘after_title’
    (string) HTML content to append to the sidebar title when displayed. Default is a closing h2 element.

Default value: array()


返回值

(string) Sidebar ID added to $wp_registered_sidebars global.


源代码

File: wp-includes/widgets.php

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

	$i = count($wp_registered_sidebars) + 1;

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

	$defaults = array(
		'name' => sprintf(__('Sidebar %d'), $i ),
		'id' => "sidebar-$i",
		'描述' => '',
		'class' => '',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => "</li>\n",
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => "</h2>\n",
	);

	$sidebar = wp_parse_args( $args, $defaults );

	if ( $id_is_empty ) {
		/* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */
		_doing_it_wrong( __FUNCTION__, sprintf( __( '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'];
}

更新日志

Version 描述
2.2.0 Introduced.

相关函数

Uses

  • wp-includes/theme.php:
    add_theme_support()
  • wp-includes/l10n.php:
    __()
  • wp-includes/functions.php:
    _doing_it_wrong()
  • wp-includes/functions.php:
    wp_parse_args()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/widgets.php:
    register_sidebar
  • Show 1 more use
    Hide more uses


Used By

  • wp-includes/widgets.php:
    register_sidebars()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by jave.web

    Sidebar id must be in lowercase ! (and no spaces)
    From codex: id – Sidebar id – Must be all in lowercase, with no spaces (default is a numeric auto-incremented ID). If you do not set the id argument value, you will get E_USER_NOTICE messages in debug mode, starting with version 4.2.

  2. This example creates a sidebar named “Main Sidebar” with and before and after the title.

    
    /**
     * Add a sidebar.
     */
    function wpdocs_theme_slug_widgets_init() {
    	register_sidebar( array(
    		'name'          => __( 'Main Sidebar', 'textdomain' ),
    		'id'            => 'sidebar-1',
    		'描述'   => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
    		'before_widget' => '<li id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</li>',
    		'before_title'  => '<h2 class="widgettitle">',
    		'after_title'   => '</h2>',
    	) );
    }
    add_action( 'widgets_init', 'wpdocs_theme_slug_widgets_init' );
    

上一篇:register_setting()

下一篇:register_sidebars()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!