2013-03-18 184 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! 
?> 

谢谢。