调用并显示自定义sidebar侧边栏
描述
此函数按顺序调用每个活动小部件回调,这将打印侧边栏的标记。如果您有多个侧边栏,则应为此功能指定要打印的侧边栏的名称或编号。此函数在成功时返回 true,在失败时返回 false。
返回值应用于确定是否显示静态侧边栏。这可确保即使 Widgets 插件未处于活动状态,您的主题也会看起来不错。
如果您的侧边栏是按编号注册的,则应按编号检索。如果他们在您注册时有姓名,请使用他们的姓名来检索他们。
用法
<?php dynamic_sidebar( $index ); ?>
参数
index
(integer/string) (可选) Name or ID of dynamic sidebar.
(整数/字符串)(可选)动态边栏的名称或 ID。
默认值: 1
示例
Here is the recommended use of this function:
以下是此函数的推荐用法:
<?php if ( is_active_sidebar( ‘left-sidebar’ ) ) : ?>
<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
<ul id=“sidebar”>
<?php dynamic_sidebar( ‘left-sidebar’ ); ?>
<?php dynamic_sidebar( 'left-sidebar' ); ?>
</ul>
<?php endif; ?> <?php endif; ?>
<ul id=“sidebar”>
<?php dynamic_sidebar( ‘right-sidebar’ ); ?>
<?php dynamic_sidebar( 'right-sidebar' ); ?>
</ul>
<ul id=“sidebar”>
<?php if ( ! dynamic_sidebar() ) : ?>
<?php if ( ! dynamic_sidebar() ) : ?>
<li>{static sidebar item 1}</li>
{静态侧边栏项目 1}
<li>{static sidebar item 2}</li>
{静态侧边栏项目 2}
<?php endif; ?> <?php endif; ?>
</ul>
在“二十一”主题(3.0+)
wp-content/themes/twentyten/sidebar.php
wp-content/themes/twentyten/sidebar-footer.php
源文件
dynamic_sidebar() 函数的代码位于 wp-includes/widgets.php
.
/**
* Display dynamic sidebar.
*
* By default this displays the default sidebar or ‘sidebar-1’. If your theme specifies the ‘id’ or
* ‘name’ parameter for its registered sidebars you can pass an id or name as the $index parameter.
* Otherwise, you can pass in a numerical index to display the sidebar at that index.
*
* @since 2.2.0
*
* @global array $wp_registered_sidebars
* @global array $wp_registered_widgets
*
* @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar.
* @return bool True, if widget sidebar was found and called. False if not found or not called.
*/
function dynamic_sidebar($index = 1) {
global $wp_registered_sidebars, $wp_registered_widgets;
if ( is_int($index) ) {
$index = “sidebar-$index”;
} else {
$index = sanitize_title($index);
foreach ( (array) $wp_registered_sidebars as $key => $value ) {
if ( sanitize_title($value[‘name’]) == $index ) {
$index = $key;
break;
}
}
}
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) {
/** This action is documented in wp-includes/widgets.php */
do_action( ‘dynamic_sidebar_before’, $index, false );
/** This action is documented in wp-includes/widgets.php */
do_action( ‘dynamic_sidebar_after’, $index, false );
/** This filter is documented in wp-includes/widgets.php */
return apply_filters( ‘dynamic_sidebar_has_widgets’, false, $index );
}
/**
* Fires before widgets are rendered in a dynamic sidebar.
*
* Note: The action also fires for empty sidebars, and on both the front-end
* and back-end, including the Inactive Widgets sidebar on the Widgets screen.
*
* @since 3.9.0
*
* @param int|string $index Index, name, or ID of the dynamic sidebar.
* @param bool $has_widgets Whether the sidebar is populated with widgets.
* Default true.
*/
do_action( ‘dynamic_sidebar_before’, $index, true );
$sidebar = $wp_registered_sidebars[$index];
$did_one = false;
foreach ( (array) $sidebars_widgets[$index] as $id ) {
if ( !isset($wp_registered_widgets[$id]) ) continue;
$params = array_merge(
array( array_merge( $sidebar, array(‘widget_id’ => $id, ‘widget_name’ => $wp_registered_widgets[$id][‘name’]) ) ),
(array) $wp_registered_widgets[$id][‘params’]
);
// Substitute HTML id and class attributes into before_widget
$classname_ = ”;
foreach ( (array) $wp_registered_widgets[$id][‘classname’] as $cn ) {
if ( is_string($cn) )
$classname_ .= ‘_’ . $cn;
elseif ( is_object($cn) )
$classname_ .= ‘_’ . get_class($cn);
}
$classname_ = ltrim($classname_, ‘_’);
$params[0][‘before_widget’] = sprintf($params[0][‘before_widget’], $id, $classname_);
/**
* Filter the parameters passed to a widget’s display callback.
*
* Note: The filter is evaluated on both the front-end and back-end,
* including for the Inactive Widgets sidebar on the Widgets screen.
*
* @since 2.5.0
*
* @see register_sidebar()
*
* @param array $params {
* @type array $args {
* An array of widget display arguments.
*
* @type string $name Name of the sidebar the widget is assigned to.
* @type string $id ID of the sidebar the widget is assigned to.
* @type string $description The sidebar description.
* @type string $class CSS class applied to the sidebar container.
* @type string $before_widget HTML markup to prepend to each widget in the sidebar.
* @type string $after_widget HTML markup to append to each widget in the sidebar.
* @type string $before_title HTML markup to prepend to the widget title when displayed.
* @type string $after_title HTML markup to append to the widget title when displayed.
* @type string $widget_id ID of the widget.
* @type string $widget_name Name of the widget.
* }
* @type array $widget_args {
* An array of multi-widget arguments.
*
* @type int $number Number increment used for multiples of the same widget.
* }
* }
*/
$params = apply_filters( ‘dynamic_sidebar_params’, $params );
$callback = $wp_registered_widgets[$id][‘callback’];
/**
* Fires before a widget’s display callback is called.
*
* Note: The action fires on both the front-end and back-end, including
* for widgets in the Inactive Widgets sidebar on the Widgets screen.
*
* The action is not fired for empty sidebars.
*
* @since 3.0.0
*
* @param array $widget_id {
* An associative array of widget arguments.
*
* @type string $name Name of the widget.
* @type string $id Widget ID.
* @type array|callback $callback When the hook is fired on the front-end, $callback is an array
* containing the widget object. Fired on the back-end, $callback
* is ‘wp_widget_control’, see $_callback.
* @type array $params An associative array of multi-widget arguments.
* @type string $classname CSS class applied to the widget container.
* @type string $description The widget description.
* @type array $_callback When the hook is fired on the back-end, $_callback is populated
* with an array containing the widget object, see $callback.
* }
*/
do_action( ‘dynamic_sidebar’, $wp_registered_widgets[ $id ] );
if ( is_callable($callback) ) {
call_user_func_array($callback, $params);
$did_one = true; $did_one = 真;
}
}
/**
* Fires after widgets are rendered in a dynamic sidebar.
* 在动态侧边栏中渲染小部件后触发。
*
* Note: The action also fires for empty sidebars, and on both the front-end
* and back-end, including the Inactive Widgets sidebar on the Widgets screen.
*
* @since 3.9.0
*
* @param int|string $index Index, name, or ID of the dynamic sidebar.
* @param bool $has_widgets Whether the sidebar is populated with widgets.
* Default true.
*/
do_action( ‘dynamic_sidebar_after’, $index, true );
do_action( 'dynamic_sidebar_after', $index, true );
/**
* Filter whether a sidebar has widgets.
*过滤侧边栏是否有小部件。
*
* Note: The filter is also evaluated for empty sidebars, and on both the front-end
*注意:过滤器也会被评估为空侧边栏,并且在两个前端
* and back-end, including the Inactive Widgets sidebar on the Widgets screen.
*
* @since 3.9.0
*
* @param bool $did_one Whether at least one widget was rendered in the sidebar.
* @param bool $did_one 侧边栏中是否至少呈现了一个小部件。
* Default false. * 默认为假。
* @param int|string $index Index, name, or ID of the dynamic sidebar.
*/
return apply_filters( ‘dynamic_sidebar_has_widgets’, $did_one, $index );
}
原文:http://codex.wordpress.org/Function_Reference/dynamic_sidebar