register_sidebar()
register_sidebar( array|string $args = array() )
为单个边栏生成定义并返回ID。
Builds the definition for a single sidebar and returns the ID.
说明(Description)
接受字符串或数组,然后根据新侧边栏的一组默认参数解析该字符串或数组。如果不包含这些参数,WordPress将根据当前注册的边栏数量自动生成边栏ID和名称。
当允许自动生成名称和ID参数时,请记住侧边栏的incrementor可能会随着时间的推移而改变,这取决于安装了哪些其他插件和主题。
如果调用此函数时尚未添加对“widgets”的主题支持,则将通过使用add_theme_support()自动启用它
返回(Return)
(string)边栏ID添加到$wp_registered_sidebars global。
源码(Source)
/** * 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() * * @since 2.2.0 * * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. * * @param 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. 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. * } * @return string Sidebar ID added to $wp_registered_sidebars global. */ 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", 'description' => '', 'class' => '', 'before_widget' => '', 'after_widget' => " ", 'before_title' => '', 'after_title' => " ", ); $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.' ), 'id', $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']; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
2.2.0 | wp-includes/widgets.php:247 | 1 function | 8 |
笔记(Notes)
要输出注册侧边栏:
本例创建了一个名为“Main sidebar”的侧边栏,标题前后都有。
边栏id必须小写!(没有空格)
register_sidebar() 为WP2原创文章,链接:https://www.wp2.cn/functions/register_sidebar/