Function Reference/register sidebar

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Description

Builds the definition for a single sidebar and returns the ID. Call on "widgets_init" action.

Usage

%%%<?php register_sidebar( $args ); ?>%%%

Default Usage

<?php $args = array(
	'name'          => __( 'Sidebar name', 'theme_text_domain' ),
	'id'            => 'unique-sidebar-id',
	'description'   => '',
        'class'         => '',
	'before_widget' => '<li id="%1$s" class="widget %2$s">',
	'after_widget'  => '</li>',
	'before_title'  => '<h2 class="widgettitle">',
	'after_title'   => '</h2>' ); ?>

Parameters

ttargs/tt
(string/array) (optional) Builds Sidebar based off of 'name' and 'id' values.
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

  • name - Sidebar name (default is localized 'Sidebar' and numeric ID).
  • id - Sidebar id - Must be all in lowercase, with no spaces (default is a numeric auto-incremented ID).
  • description - Text description of what/where the sidebar is. Shown on widget management screen. (Since 2.9) (default: empty)
  • class - CSS class name to assign to the widget HTML (default: empty).
  • before_widget - HTML to place before every widget(default: '<li id="%1$s" class="widget %2$s">') Note: uses sprintf for variable substitution
  • after_widget - HTML to place after every widget (default: "</li>\n").
  • before_title - HTML to place before every title (default: <h2 class="widgettitle">).
  • after_title - HTML to place after every title (default: "</h2>\n").

The optional args parameter is an associative array that will be passed as a first argument to every active widget callback. (If a string is passed instead of an array, it will be passed through parse_str() to generate an associative array.) The basic use for these arguments is to pass theme-specific HTML tags to wrap the widget and its title.

Notes

  • With WordPress 3.4.1 there're still some IDs to avoid, that can be found here. Props to "toscho" for building a plugin collecting and listing them.
  • Calling register_sidebar() multiple times to register a number of sidebars is preferable to using register_sidebars() to create a bunch in one go, because it allows you to assign a unique name to each sidebar (eg: “Right Sidebar”, “Left Sidebar”). Although these names only appear in the admin interface it is a best practice to name each sidebar specifically, giving the administrative user some idea as to the context for which each sidebar will be used.
  • The default before/after values are intended for themes that generate a sidebar marked up as a list with h2 titles. This is the convention we recommend for all themes and any theme built in this way can simply register sidebars without worrying about the before/after tags. If, for some compelling reason, a theme cannot be marked up in this way, these tags must be specified when registering sidebars. It is recommended to copy the id and class attributes verbatim so that an internal sprintf call can work and CSS styles can be applied to individual widgets.

Example

This will create a sidebar named "RightSideBar" with <h1> and </h1> before and after the title:

register_sidebar(array(
  'name' => __( 'Right Hand Sidebar' ),
  'id' => 'right-sidebar',
  'description' => __( 'Widgets in this area will be shown on the right-hand side.' ),
  'before_title' => '<h1>',
  'after_title' => '</h1>'
));

Change Log

Source File

register_sidebar() is located in onlyincludecodewp-includes/widgets.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Resources

Related

模板:Sidebar Tags

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude