I am using Wordpress and PHP. I declared a custom sidebar using this code :
function customtheme_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'My custom sidebar', 'customtheme' ),
'id' => 'my-custom-sidebar',
'description' => esc_html__( '.', 'customtheme' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'customtheme_widgets_init' );
Ok so In my code I want to get my sidebar and store it in a PHP variable $the_widget . Using this code :
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
$the_widget = dynamic_sidebar('my-custom-sidebar');
endif;
when i do that it automatically echoes the widget(sidebar) when i call dynamic_sidebar() can i do that without the echo?? is there another function in the wordpress codex that can do the same?
No unfortunately there isn't a WordPress function such as get_dynamic_sidebar()
however a common method for getting the sidebar into a variable as a string is this:
if ( is_active_sidebar( 'my-custom-sidebar' ) ) :
ob_start();
dynamic_sidebar('my-custom-sidebar');
$the_widget = ob_get_contents(); //or ob_get_clean();
ob_end_clean();
endif;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.