首页 > 解决方案 > Wordpress 动态页脚小部件 - 仅当一个小部件存在时才显示第二个小部件区域,否则显示 1

问题描述

我正在我的 WordPress 主题的页脚中创建一个动态小部件区域。

我想要做的是,创建 4 个小部件,但仅在存在一个时才显示下一个。

所以默认情况下,我会看到一个名为 footer one 的小部件

如果我们添加页脚,则会显示一个页脚 2 选项,但是在页脚中,我们将看到一个页脚小部件如果我们在页脚 2 中添加第二个小部件,我们将看到小部件 3。如果我们在页脚 3 中添加一个小部件,我们将看到页脚4.

如果只有页脚 1 存在宽度 100% 如果 2 存在页脚 1 50%,我需要设置样式。页脚 2 50%

如果存在 3 页脚 1 33% 页脚 2 33% 。页脚 3 33%

如果存在 4 页脚 1 25% 页脚 2 25% 页脚 3 25% 页脚 4 25%

重要的是,我设法完成了这项工作。但如果我仍然无法让其他小部件仅在父小部件被填充时显示。

这是我的代码:

    <div class="footer-widgets">

    <?php
    /* The footer widget area is triggered if any of the areas
     * have widgets. So let's check that first.
     *
     * If none of the sidebars have widgets, then let's bail early.
     */
    if (   ! is_active_sidebar( 'first-footer-widget-area'  )
        && ! is_active_sidebar( 'second-footer-widget-area' )
        && ! is_active_sidebar( 'third-footer-widget-area'  )
        && ! is_active_sidebar( 'fourth-footer-widget-area' )
    )
        return;

    if (   is_active_sidebar( 'first-footer-widget-area'  )
        && is_active_sidebar( 'second-footer-widget-area' )
        && is_active_sidebar( 'third-footer-widget-area'  )
        && is_active_sidebar( 'fourth-footer-widget-area' )
    ) : ?>

    <aside class="fatfooter" role="complementary">
        <div class="first quarter left widget-area">
            <?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
        </div><!-- .first .widget-area -->

        <div class="second quarter widget-area">
            <?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
        </div><!-- .second .widget-area -->

        <div class="third quarter widget-area">
            <?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
        </div><!-- .third .widget-area -->

        <div class="fourth quarter right widget-area">
            <?php dynamic_sidebar( 'fourth-footer-widget-area' ); ?>
        </div><!-- .fourth .widget-area -->
    </aside><!-- #fatfooter -->

    <?php 
    elseif ( is_active_sidebar( 'first-footer-widget-area'  )
        && is_active_sidebar( 'second-footer-widget-area' )
        && is_active_sidebar( 'third-footer-widget-area'  )
        && ! is_active_sidebar( 'fourth-footer-widget-area' )
    ) : ?>
    <aside class="fatfooter" role="complementary">
        <div class="first one-third left widget-area">
            <?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
        </div><!-- .first .widget-area -->

        <div class="second one-third widget-area">
            <?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
        </div><!-- .second .widget-area -->

        <div class="third one-third right widget-area">
            <?php dynamic_sidebar( 'third-footer-widget-area' ); ?>
        </div><!-- .third .widget-area -->

    </aside><!-- #fatfooter -->

    <?php
    elseif ( is_active_sidebar( 'first-footer-widget-area'  )
        && is_active_sidebar( 'second-footer-widget-area' )
        && ! is_active_sidebar( 'third-footer-widget-area'  )
        && ! is_active_sidebar( 'fourth-footer-widget-area' )
    ) : ?>
    <aside class="fatfooter" role="complementary">
        <div class="first half left widget-area">
            <?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
        </div><!-- .first .widget-area -->

        <div class="second half right widget-area">
            <?php dynamic_sidebar( 'second-footer-widget-area' ); ?>
        </div><!-- .second .widget-area -->

    </aside><!-- #fatfooter -->

    <?php
    elseif ( is_active_sidebar( 'first-footer-widget-area'  )
        && ! is_active_sidebar( 'second-footer-widget-area' )
        && ! is_active_sidebar( 'third-footer-widget-area'  )
        && ! is_active_sidebar( 'fourth-footer-widget-area' )
    ) :
    ?>
    <aside class="fatfooter" role="complementary">
        <div class="first full-width widget-area">
            <?php dynamic_sidebar( 'first-footer-widget-area' ); ?>
        </div><!-- .first .widget-area -->

    </aside><!-- #fatfooter -->


    <?php //end of all sidebar checks.
    endif;?>

</div>

标签: phphtmlcsswordpresswidget

解决方案


您可以在 Wordpress PHP 中使用 If else 并可以在其基础上打印 CSS...只需参考一个返回活动小部件数量的钩子...

这可以帮助您构建一个函数来获取活动小部件:https ://generatewp.com/snippet/2V0V0gy/

谢谢!


推荐阅读