【问题标题】:Hide sidebar if empty - WordPress如果为空则隐藏侧边栏 - WordPress
【发布时间】:2017-02-12 09:26:29
【问题描述】:

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

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

【问题讨论】:

    标签: wordpress hide sidebar


    【解决方案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 标记将仅在小部件处于活动状态时打印。

    【讨论】:

    • 它不适用于基于块的小部件
    【解决方案2】:

    O 认为这可以解决问题:

    <?php is_active_sidebar( $index ); ?>
    

    但是,根据我对 WordPress 的很少经验,要么有侧边栏,要么没有。取决于您选择的模板天气它是否有侧边栏......我认为这样效果更好

    【讨论】:

    • 我得到了这个
    • 不,我要在这件上拔头发了。目前我正在尝试用 get_sidebar 替换 dynamic_sidebar
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2021-08-12
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多