Languages: English • Русский • 日本語 (Add your language)
This function calls each of the active widget callbacks in order, which prints the markup for the sidebar. If you have more than one sidebar, you should give this function the name or number of the sidebar you want to print. This function returns true on success and false on failure.
The return value should be used to determine whether to display a static sidebar. This ensures that your theme will look good even when the Widgets plug-in is not active.
If your sidebars were registered by number, they should be retrieved by number. If they had names when you registered them, use their names to retrieve them.
<?php dynamic_sidebar( $index ); ?>
Here is the recommended use of this function:
<?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?> <ul id="sidebar"> <?php dynamic_sidebar( 'left-sidebar' ); ?> </ul> <?php endif; ?>
<ul id="sidebar"> <?php dynamic_sidebar( 'right-sidebar' ); ?> </ul>
<ul id="sidebar"> <?php if ( ! dynamic_sidebar() ) : ?> <li>{static sidebar item 1}</li> <li>{static sidebar item 2}</li> <?php endif; ?> </ul>
You can load a specific sidebar by either their name (if given a string) or ID (if given an integer). For example, dynamic_sidebar('top_menu') will present a sidebar registered with register_sidebar(array('name'=>'top_menu',)).
Using ID's ( dynamic_sidebar(1) ) is easier in that you don't need to name your sidebar, but they are harder to figure out without looking into your functions.php file or in the widgets administration panel and thus make your code less readable. Note that ID's begin at 1.
If you name your own ID values in the register_sidebar() WordPress function, you might increase readability of the code. The ID should be all lowercase alphanumeric characters and not contain white space. You can also use the - and _ characters. IDs must be unique and cannot match a sidebar name. Using your own IDs can also make the sidebar name translatable.
// See the __() WordPress function for valid values for $text_domain. register_sidebar( array( 'id' => 'top-menu', 'name' => __( 'Top Menu', $text_domain ), 'description' => __( 'This sidebar is located above the age logo.', $text_domain ), ) );
This allows the use of dynamic_sidebar( 'top-menu' ) which uses an ID and is readable.
Since: 2.2.0
dynamic_sidebar() is located in wp-includes/widgets.php
.
Sidebars: is_active_sidebar(), is_dynamic_sidebar(), dynamic_sidebar(), register_sidebars(), register_sidebar(), unregister_sidebar(), wp_register_sidebar_widget(), wp_unregister_sidebar_widget(), wp_get_sidebars_widgets(), wp_set_sidebars_widgets()