2013-03-18 142 views
2

我試圖在Wordpress中輸出一個側邊欄,具體取決於它是否具有窗口小部件(處於活動狀態),並將其顯示在我的Shop頁面上。IF,ELSE和ELSEIF語句

我對PHP很不熟悉,到目前爲止已經寫了下面的腳本來嘗試做這個工作,但似乎並沒有發生。

shop.php

<?php 
/** 
* The template for displaying the Shop page. 
*/ 

get_header(); ?> 
    <div id="primary" class="site-content"> 
     <div id="content" role="main"> 
      <?php shop_content(); ?> 
     </div><!-- #content --> 
    </div><!-- #primary --> 

<?php get_sidebar('shop'); ?> 
<?php get_footer(); ?> 

側邊欄shop.php

<?php 
/** 
* The sidebar containing the Shop page widget areas. 
* 
* If no active widgets are in the sidebars (Left, Right and theShop), 
* the sidebars should be hidden completely. 
*/ 
?> 

    <?php 
    // If the left, shop and right sidebars are inactive 
if (! is_active_sidebar('right-sidebar') && ! is_active_sidebar('shop-sidebar')) { 
    return; 
} 

    // If there are active widgets in the Shop Sidebar 
    if (is_active_sidebar('shop-sidebar')) { ?> 
     <div id="secondary" class="widget-area" role="complementary"> 
      <?php dynamic_sidebar('shop-sidebar'); ?> 
     </div><!-- #secondary --> 
    <?php 
    } 

    // If there are active widgets in the Right Sidebar 
    elseif (is_active_sidebar('right-sidebar')) { ?> 
     <div id="secondary" class="widget-area" role="complementary"> 
      <?php dynamic_sidebar('right-sidebar'); ?> 
     </div><!-- #secondary --> 
    <?php 
    } 
    ?> 

的sidebar.php

<?php 
/** 
* The sidebar containing the main widget area. 
* 
* If no active widgets in sidebar, let's hide it completely. 
* 
*/ 
?> 

    <?php if (is_active_sidebar('right-sidebar')) : ?> 
     <div id="secondary" class="widget-area" role="complementary"> 
      <?php dynamic_sidebar('right-sidebar'); ?> 
     </div><!-- #secondary --> 
    <?php endif; ?> 

我將如何莫dify上面的腳本,以輸出如下:

  • 如果右側欄(右側欄)具有窗口小部件,顯示右側欄
  • 如果商店側欄(店鋪側欄)具有窗口小部件,顯示該商店邊欄
  • 如果右邊欄和側欄店都有小部件,顯示店鋪側邊欄
  • 如果既不是右邊欄或側邊欄鋪有任何部件,不顯示任何

謝謝。

回答

0
// if both are active shoe shop 
if (is_active_sidebar('shop') && is_active_sidebar('sidebar-1')) { 
     get_sidebar('shop'); 
} 
//if shop is active, show shop 
elseif (is_active_sidebar('shop')) { 
    get_sidebar('shop'); 
} 
// if sidebar 1 is active show it. 
elseif (is_active_sidebar('sidebar-1')) { 
    get_sidebar('sidebar-1'); 
} 
0

代碼應該很簡單,因爲您已經在右側邊欄上定義了商店側欄的優先順序。如果您不需要顯示側邊欄,則無需擔心。

<?php 

// If shop sidebar is active, it takes precedence 
if (is_active_sidebar('shop')){ 
    get_sidebar('shop'); 
} 
elseif (is_active_sidebar('sidebar-1')){ 
    get_sidebar('sidebar-1'); 
    //This is "shop side bar is inactive and right sidebar is active" 
} 

// That's all; just two condition blocks! 
?> 

謝謝。