在Wordpress中显示活动的侧边栏


Show active sidebar in Wordpress

所以如果没有要显示的小部件,我试图不显示侧边栏

当我使用 is_active_sidebar() 函数时,它总是返回

false,if 语句不起作用,当我尝试使用 is_dynamic_sidebar() 函数时,它总是返回 true。

我安装了小部件逻辑插件,因此某些小部件

显示在页面上,而某些页面没有小部件。

这是我的代码:

        <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>

关于我应该做什么的任何想法?

您使用了错误的功能来检查活动侧边栏 试试这个

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

试试这个,这是使用侧边栏的不同方式

[1] 将所有"left_bar"(侧边栏)代码放入名为"sidebar-left_bar"的新文件中

[2] 将其与标头.php、函数.php和所有文件一起保存

[3] 现在只需在您想使用的地方使用<?php get_sidebar( 'left_bar' ); ?>

谢谢