簡體   English   中英

WordPress動態頁腳小部件-僅在存在一個小部件時顯示第二個小部件區域,否則顯示1

[英]Wordpress dynamic footer widgets - Show second widget area only if one widget is existing otherwise show 1

我在WordPress主題的頁腳中創建了一個動態小部件區域。

我想做的是,創建4個小部件,但僅在存在一個小部件時顯示下一個。

因此默認情況下,我將看到一個稱為頁腳1的小部件

如果添加頁腳,則會顯示一個頁腳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>

您可以在Wordpress PHP中使用If else並可以在其基礎上打印CSS ...只需參考一個返回活動小部件數量的鈎子即可。

這可以幫助您構建一個功能來獲取活動的窗口小部件: https : //generatewp.com/snippet/2V0V0gy/

謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM