Sidebar shortcode
-
Hello,
I am trying to create a shortcode for the wordpress default sidebar so it can be added everywhere in the page.
So i created the sidebar:
// Sidebar Shortcode function shortcode_sidebar( $atts, $content = null ) { extract(shortcode_atts(array( 'type' => 'vertical' ), $atts)); if ($type == 'horizontal') { include(TEMPLATEPATH."/footer_sidebar.php"); } else { get_sidebar(); } }
And when I add it in the editor, the sidebar appears as it was projected to appear just that if I place it in a <div> container
<div class="sidebar-container">[sidebar]</div>
The sidebar is taken out of the div container and is listed above.
Do you have any explanation for that?
Thanks
-
anything in a shortcode needs to be returned instead of printed/echoed – however, the call of
get_sidebar()
callsdynamic_sidebar()
which directly outputs the widgets.you could try to work with output buffering; http://php.net/manual/en/book.outcontrol.php
With alchymyth’s help I managed to rewrite the code as follows:
// Sidebar Shortcode function shortcode_sidebar( $atts, $content = null ) { extract(shortcode_atts(array( 'type' => 'vertical' ), $atts)); if ($type == 'horizontal') { ob_start(); include(TEMPLATEPATH."/footer_sidebar.php"); $string_to_return = ob_get_contents(); ob_end_clean(); return $string_to_return; } else { ob_start(); get_sidebar(); $string_to_return = ob_get_contents(); ob_end_clean(); return $string_to_return; } } add_shortcode('sidebar', 'shortcode_sidebar');
And it works.
Thanks a lot!
- The topic ‘Sidebar shortcode’ is closed to new replies.