Add WordPress sidebar using shortcode – conflicts with following custom meta

Trying to add a sidebar by creating and using a shortcode. I can get the sidebar to show, but it then causes issues with custom meta that creates buttons at the bottom of the page, after the_content();. This is the code to create the shortcode [sidebar]:

function sidebar_sc( $atts ) {
ob_start();
dynamic_sidebar('Sidebar');
$html = ob_get_contents();
ob_end_clean();
return '
<aside id="sidebar">'.$html.'</aside>';
}
add_shortcode('sidebar', 'sidebar_sc');

Read More

My page template has this:

<?php echo the_content(); ?>
<?php if($meta_button_t_1 || $meta_button_t_2 || $meta_button_t_3) { ?>
<?php get_template_part('section', 'pageButtons'); ?>
<?php } ?>

And the template part ‘pageButtons’ has this:

<div id="page_buttons" class="<?php echo $class; ?>">
<?php if($meta_button_t_1) { ?>
<a href="<?php echo $meta_button_h_1; ?>" class="cr_btn_large" title="The Revolving Stage Co: <?php echo $meta_button_t_1; ?>" id="page_button_1"><?php echo $meta_button_t_1; ?></a>
<?php } if($meta_button_t_2) { ?>
<a href="<?php echo $meta_button_h_2; ?>" class="cr_btn_large" title="The Revolving Stage Co: <?php echo $meta_button_t_2; ?>" id="page_button_2"><?php echo $meta_button_t_2; ?></a>
<?php } if($meta_button_t_3) { ?>
<a href="<?php echo $meta_button_h_3; ?>" class="cr_btn_large" title="The Revolving Stage Co: <?php echo $meta_button_t_3; ?>" id="page_button_3"><?php echo $meta_button_t_3; ?></a>
<?php } ?>
</div>

Without using the [sidebar] shortcode, the buttons show fine. If I include the shortcode, then I get the div but it’s empty. If I include the template part before the_content();, everything works except it’s obviously in the wrong place.

Related posts

Leave a Reply