从您的问题中,您已经注册了侧栏。
要将侧栏的内容放入变量中,我认为可以使用php缓冲:
// 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
例如:
// 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();
如果需要获取多个侧栏内容,例如需要同时获取多个侧栏的内容,则可以:
// 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;
或
// 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;
希望这一点对你有帮助。