admin管理员组文章数量:1122832
I have created a custom sidebar widgets using below code :
register_sidebar(array(
'id' => 'sidebar-widget-1',
'name' => 'Sidebar Widget 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
and it is showing in Appearance -> Widgets
and it is also showing content on frontend using dynamic_sidebar('Sidebar Widget 1')
.
But I want to get content of this register_sidebar
by using its id
into a variable.
How to get sidebar content by using its id
?
I have created a custom sidebar widgets using below code :
register_sidebar(array(
'id' => 'sidebar-widget-1',
'name' => 'Sidebar Widget 1',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
and it is showing in Appearance -> Widgets
and it is also showing content on frontend using dynamic_sidebar('Sidebar Widget 1')
.
But I want to get content of this register_sidebar
by using its id
into a variable.
How to get sidebar content by using its id
?
2 Answers
Reset to default 0In your sidebar.php or any other custom sidebar file call by ID like this
<?php get_sidebar('sidebar-widget-1'); ?>
Not like this
<?php get_sidebar('Sidebar Widget 1'); ?>
See the difference, I used ID in get_sidebar()
function instead of name.
Now where ever you want sidebar appear in front-end, there call it like this
<?php get_sidebar('sidebar-widget-1'); ?>
From your question you have already registered the sidebar.
To get the content of the sidebar into a variable, i think you can use php buffering :
// ob_start - Turn on output buffering
// ob_get_contents - Return the contents of the output buffer
// ob_end_clean - Clean (erase) the output buffer and turn off output buffering
Such as this :
// Turn on output buffering
ob_start();
// Specify the sidebar using its [id]
dynamic_sidebar('sidebar-widget-1');
// Store the return contents of the output buffer in a variable
// in this case $sidebar_content
$sidebar_content = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
if you need to get more than one sidebar content, like you need the get the content of several sidebars at once then you can:
// Get the list of all registered sidebars using wp_registered_sidebars
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) :
// Turn on output buffering
ob_start();
// This will get all the sidebars
dynamic_sidebar($sidebar['id']);
// Store the return contents of the output buffer in a variable
// in this case $sidebars_content
$sidebars_content = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
endif;
OR
// Get the global registered sidebars
global $wp_registered_sidebars;
// Loop through each content in $wp_registered_sidebars array
foreach($wp_registered_sidebars as $sidebar_id => $sidebar) :
ob_start();
// This will get all the sidebars
dynamic_sidebar($sidebar['id']);
// Store the return contents of the output buffer in a variable
// in this case $sidebars_content
$sidebars_content = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
endforeach;
Hope one on this helps you.
本文标签: How to get sidebar widgets in leftsidebar template
版权声明:本文标题:How to get sidebar widgets in leftsidebar template 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282649a1926712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论