隐藏侧栏如果为空 - WordPress

时间:2013-11-18 11:49:41

标签: wordpress hide sidebar

如果没有内容,我想隐藏我的侧边栏,不幸的是有26000个函数可以调用你的侧边栏,我真的不感兴趣。我想要的是隐藏我的侧边栏,如果它们是空的,并且在它们包含小部件时显示它们。

现在我不确定是否应该使用get_sidebar函数或dynamic_sidebar函数。另外我想知道我是否应该使用sidebar-name.php来调用它或者函数中的ID .php

2 个答案:

答案 0 :(得分:1)

我刚刚在我的WordPress网站上应用了这个功能。

<?php if ( is_active_sidebar( 'left-sidebar' ) ) { ?>
    <ul id="sidebar">
        <?php dynamic_sidebar( 'left-sidebar' ); ?>
    </ul>
<?php } ?>

在我的情况下,我在我的functions.php

上有这个
function videobgFour() {
register_sidebar( array(
'name' => 'Background Four',
'id'   => 'videobg4',
));
}
add_action('widgets_init', 'videobgFour');

这是我的header.php文件

<div class="box">
<?php dynamic_sidebar('videobg4'); ?>
</div>

所以我必须以这种方式使用PHP代码。

<?php if ( is_active_sidebar( 'videobg4' ) ) { ?>
    <div class="box">
       <?php dynamic_sidebar('videobg4'); ?>
    </div>
<?php } ?>

在这种情况下,HTML标记仅在窗口小部件处于活动状态时才会打印。

答案 1 :(得分:0)

我认为这样可以解决问题:

<?php is_active_sidebar( $index ); ?>

然而,在我使用WordPress的经验中,要么你有侧边栏,要么你没有。取决于您选择的模板天气它是否有侧边栏...我认为它的效果更好