Show active sidebar in Wordpress

自古美人都是妖i 提交于 2019-12-11 13:09:29

问题


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



来源:https://stackoverflow.com/questions/19374149/show-active-sidebar-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!