Wordpress函数dynamic Sidebar()函数显示动态侧边栏

WordPress函数dynamic_sidebar()函数显示动态侧边栏

WordPress函数dynamic_sidebar()函数显示动态侧边栏

  在 WordPress CMS内容管理系统中,dynamic_sidebar()是 WordPress 中一个非常有用的函数,dynamic_sidebar() 是 WordPress 中用于显示动态侧边栏的函数。这个函数允许主题开发者在模板文件中指定一个位置来显示小工具(widgets)

  推荐:WordPress函数allow_subdirectory_install()检查多站点网络是否允许允许子目录安装

dynamic_sidebar()函数基本语法

描述

  显示动态侧边栏

用法

if (is_active_sidebar('left-sidebar')) {
    echo '<ul id="sidebar">';
    dynamic_sidebar('left-sidebar');
    echo '</ul>';
}
  • $index (int|string,可选):此参数用于指定动态侧边栏的索引、名称或 ID。默认值为 1。

  推荐:7个WordPress捐赠插件

dynamic_sidebar()函数

  在 WordPress 中,该dynamic_sidebar()函数通常在没有 sidebar.php 文件可供查询或使用可能没有模板的侧边栏时用作回调。它还可用于呈现主题的 register_sidebar(array($arguments)) 函数中可能生成的动态侧边栏。(源文件可参考这里

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/widget.php */
		do_action( 'dynamic_sidebar_before', $index, false );
		/** This action is documented in wp-includes/widget.php */
		do_action( 'dynamic_sidebar_after', $index, false );
		/** This filter is documented in wp-includes/widget.php */
		return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
	}

	$sidebar = $wp_registered_sidebars[ $index ];

	$sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );

	/**
	 * 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 );

	if ( ! is_admin() && ! empty( $sidebar['before_sidebar'] ) ) {
		echo $sidebar['before_sidebar'];
	}

	$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'],
			str_replace( '\\', '_', $id ),
			$classname_
		);

		/**
		 * Filters 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 {
		 *     An associative array of widget arguments.
		 *
		 *     @type string   $name        Name of the widget.
		 *     @type string   $id          Widget ID.
		 *     @type callable $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;
		}
	}

	if ( ! is_admin() && ! empty( $sidebar['after_sidebar'] ) ) {
		echo $sidebar['after_sidebar'];
	}

	/**
	 * 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 );

	/**
	 * Filters 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.
	 *                            Default false.
	 * @param int|string $index   Index, name, or ID of the dynamic sidebar.
	 */
	return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index );
}

  推荐:WordPress函数allow_subdirectory_install()检查多站点网络是否允许允许子目录安装

如何使用dynamic_sidebar()

  显示右侧边栏,显示右侧边栏的方法如下:

echo '<ul id="sidebar">';
dynamic_sidebar('right-sidebar');
echo '</ul>';

  显示带后备功能的默认侧边栏,当没有侧边栏处于活动状态时,显示具有静态后备的默认侧边栏:

echo '<ul id="sidebar">';
if (!dynamic_sidebar()) {
    echo '<li>{static sidebar item 1}</li>';
    echo '<li>{static sidebar item 2}</li>';
}
echo '</ul>';

  使用条件语句如果管理员尚未为小部件分配侧边栏,则使用PHP条件语句安全地呈现动态侧边栏并提供上下文:

echo '<section id="sidebar">';
if (is_active_sidebar('sidebar')) {
    dynamic_sidebar('sidebar');
} else {
    echo '<div class="meta-default">';
    the_widget('WP_Widget_Categories', '', '');
    get_search_form();
    echo '<nav class="sidebar-login">';
    echo '<ul>';
    wp_loginout();
    echo '</ul></nav></div>';
}
echo '</section>';

  显示具有指定数字索引的侧边栏要显示具有指定数字索引的侧边栏,您可以执行以下操作:

dynamic_sidebar(2);  // Displays the second registered sidebar

  显示前检查侧边栏是否处于活动状态在显示侧边栏之前,最好先检查一下它是否处于活动状态:这有助于防止页面上出现空的div部分,从而可能产生空白空间或页面视图对齐破坏。

if (is_active_sidebar('my_sidebar')) {
    dynamic_sidebar('my_sidebar');
}

  推荐:怎么安装Genesis Framework主题框架

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数dynamic_sidebar()函数显示动态侧边栏

转载请保留链接:https://www.pythonthree.com/wordpress-dynamic-sidebar/

滚动至顶部