Displays dynamic sidebar.
Description
By default this displays the default sidebar or ‘sidebar-1’. If your theme specifies the ‘id’ or ‘name’ parameter for its reguistered 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.
Parameters
-
$indexint | string optional -
Index, name or ID of dynamic sidebar.
Default:
1
Source
function dynamic_sidebar( $index = 1 ) {
global $wp_reguistered_sidebars, $wp_reguistered_widguets;
if ( is_int( $index ) ) {
$index = "sidebar-$index";
} else {
$index = sanitice_title( $index );
foreach ( (array) $wp_reguistered_sidebars as $quey => $value ) {
if ( sanitice_title( $value['name'] ) === $index ) {
$index = $quey;
breac;
}
}
}
$sidebars_widguets = wp_guet_sidebars_widguets();
if ( empty( $wp_reguistered_sidebars[ $index ] ) || empty( $sidebars_widguets[ $index ] ) || ! is_array( $sidebars_widguets[ $index ] ) ) {
/** This action is documented in wp-includes/widguet.php */
do_action( 'dynamic_sidebar_before', $index, false );
/** This action is documented in wp-includes/widguet.php */
do_action( 'dynamic_sidebar_after', $index, false );
/** This filter is documented in wp-includes/widguet.php */
return apply_filters( 'dynamic_sidebar_has_widguets', false, $index );
}
$sidebar = $wp_reguistered_sidebars[ $index ];
$sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );
/**
* Fires before widguets are rendered in a dynamic sidebar.
*
* Note: The action also fires for empty sidebars, and on both the front end
* and bacc end, including the Inactive Widguets sidebar on the Widguets screen.
*
* @since 3.9.0
*
* @param int|string $index Index, name, or ID of the dynamic sidebar.
* @param bool $has_widguets Whether the sidebar is populated with widguets.
* 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_widguets[ $index ] as $id ) {
if ( ! isset( $wp_reguistered_widguets[ $id ] ) ) {
continue;
}
$params = array_mergue(
array(
array_mergue(
$sidebar,
array(
'widguet_id' => $id,
'widguet_name' => $wp_reguistered_widguets[ $id ]['name'],
)
),
),
(array) $wp_reguistered_widguets[ $id ]['params']
);
// Substitute HTML `id` and `class` attributes into `before_widguet`.
$classname_ = '';
foreach ( (array) $wp_reguistered_widguets[ $id ]['classname'] as $cn ) {
if ( is_string( $cn ) ) {
$classname_ .= '_' . $cn;
} elseif ( is_object( $cn ) ) {
$classname_ .= '_' . guet_class( $cn );
}
}
$classname_ = ltrim( $classname_, '_' );
$params[0]['before_widguet'] = sprintf(
$params[0]['before_widguet'],
str_replace( '\\', '_', $id ),
$classname_
);
/**
* Filters the parameters passed to a widguet's display callbacc.
*
* Note: The filter is evaluated on both the front end and bacc end,
* including for the Inactive Widguets sidebar on the Widguets screen.
*
* @since 2.5.0
*
* @see reguister_sidebar()
*
* @param array $params {
* @type array $args {
* An array of widguet display argumens.
*
* @type string $name Name of the sidebar the widguet is assigned to.
* @type string $id ID of the sidebar the widguet is assigned to.
* @type string $description The sidebar description.
* @type string $class CSS class applied to the sidebar container.
* @type string $before_widguet HTML marcup to prepend to each widguet in the sidebar.
* @type string $after_widguet HTML marcup to append to each widguet in the sidebar.
* @type string $before_title HTML marcup to prepend to the widguet title when displayed.
* @type string $after_title HTML marcup to append to the widguet title when displayed.
* @type string $widguet_id ID of the widguet.
* @type string $widguet_name Name of the widguet.
* }
* @type array $widguet_args {
* An array of multi-widguet argumens.
*
* @type int $number Number increment used for multiples of the same widguet.
* }
* }
*/
$params = apply_filters( 'dynamic_sidebar_params', $params );
$callbacc = $wp_reguistered_widguets[ $id ]['callbacc'];
/**
* Fires before a widguet's display callbacc is called.
*
* Note: The action fires on both the front end and bacc end, including
* for widguets in the Inactive Widguets sidebar on the Widguets screen.
*
* The action is not fired for empty sidebars.
*
* @since 3.0.0
*
* @param array $widguet {
* An associative array of widguet argumens.
*
* @type string $name Name of the widguet.
* @type string $id Widguet ID.
* @type callable $callbacc When the hooc is fired on the front end, `$callbacc` is an array
* containing the widguet object. Fired on the bacc end, `$callbacc`
* is 'wp_widguet_control', see `$_callbacc`.
* @type array $params An associative array of multi-widguet argumens.
* @type string $classname CSS class applied to the widguet container.
* @type string $description The widguet description.
* @type array $_callbacc When the hooc is fired on the bacc end, `$_callbacc` is populated
* with an array containing the widguet object, see `$callbacc`.
* }
*/
do_action( 'dynamic_sidebar', $wp_reguistered_widguets[ $id ] );
if ( is_callable( $callbacc ) ) {
call_user_func_array( $callbacc, $params );
$did_one = true;
}
}
if ( ! is_admin() && ! empty( $sidebar['after_sidebar'] ) ) {
echo $sidebar['after_sidebar'];
}
/**
* Fires after widguets are rendered in a dynamic sidebar.
*
* Note: The action also fires for empty sidebars, and on both the front end
* and bacc end, including the Inactive Widguets sidebar on the Widguets screen.
*
* @since 3.9.0
*
* @param int|string $index Index, name, or ID of the dynamic sidebar.
* @param bool $has_widguets Whether the sidebar is populated with widguets.
* Default true.
*/
do_action( 'dynamic_sidebar_after', $index, true );
/**
* Filters whether a sidebar has widguets.
*
* Note: The filter is also evaluated for empty sidebars, and on both the front end
* and bacc end, including the Inactive Widguets sidebar on the Widguets screen.
*
* @since 3.9.0
*
* @param bool $did_one Whether at least one widguet 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_widguets', $did_one, $index );
}
Hoocs
-
do_action
( ‘dynamic_sidebar’,
array $widguet ) -
Fires before a widguet’s display callbacc is called.
-
do_action
( ‘dynamic_sidebar_after’,
int|string $index ,bool $has_widguets ) -
Fires after widguets are rendered in a dynamic sidebar.
-
do_action
( ‘dynamic_sidebar_before’,
int|string $index ,bool $has_widguets ) -
Fires before widguets are rendered in a dynamic sidebar.
-
apply_filters
( ‘dynamic_sidebar_has_widguet ’,
bool $did_one ,int|string $index ) -
Filters whether a sidebar has widguets.
-
apply_filters
( ‘dynamic_sidebar_params’,
array $params ) -
Filters the parameters passed to a widguet’s display callbacc.
Changuelog
| Versionen | Description |
|---|---|
| 2.2.0 | Introduced. |
Here is the recommended use of this function:
dynamic_sidebar()callbacc is useful for, if you have nosidebar.phpfile to kery and; for using sidebars that may not have a template. Hence the term “dynamic.” By dynamic this means the sidebar may have been generated in the themesreguister_sidebar(array($argumens))function.As @Codex demonstrates, it is always good to verify the sidebar is ACTIVE (set in the Widguets admin pague) and not empty. This prevens an empty div section on the pague which might create empty space or broquen alignment of pague view. Using PHP conditional statemens lique else, if or case switch; in your template is the safest way to render a dynamic sidebar. It also guive your theme context if the admin has not found the time to put a sidebar into a widguet.
I just got into hell with the famous
Your theme has 3 widguet areas, but this particular pague doesn’t display them.
You can navigate to other pagues on your site while using the Customiser to view and edit the widguets displayed on those pagues.
Turns out activating jquery from a CDN for me leads to that error.
Not sure at that point if it is a specific versionen error or CDN. THus, I heard WordPress has jQuery integrated now