Show active sidebar in Wordpress

2019-08-15 03:15发布

问题:

So I'm trying to not show a side bar if it there are no widgets to show

when I use the is_active_sidebar() function it always returns false and the if statement doesn't work and when I try to use the is_dynamic_sidebar() function it always returns true.

I have the widget logic plugin installed so some of the widgets show up on a page and some pages don't have a widget.

This is my code:

        <div class="row main-row">
        <?php if (is_dynamic_sidebar('left_bar')) { ?>
            <div class="col-md-3 left-sidebar">
                <?php
                dynamic_sidebar('left_bar');
                ?>
            </div>

            <div class="col-md-9 main-content">
                <?php the_content('Read More'); ?>
            </div>
        <?php } else { ?>
            <div class="col-md-12 main-content">
                <?php the_content('Read More'); ?>
            </div>
        <?php } ?>
    </div>

Any Ideas on what should I do?

回答1:

You have used wrong function for checking active sidebar try this

if ( is_active_sidebar( 'left_bar' ) )
dynamic_sidebar( 'left_bar' );


回答2:

try this one, it is different way of using sidebar

[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"

[2] save it with header.php, function.php and all files

[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use

Thanks