【问题标题】:IF, ELSE and ELSEIF StatementsIF、ELSE 和 ELSEIF 语句
【发布时间】:2013-03-18 06:28:05
【问题描述】:

我正在尝试在 Wordpress 中输出一个侧边栏,具体取决于它是否具有小部件(处于活动状态)并相应地显示在我的商店页面上。

我对 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(); ?>

sidebar-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; ?>

如何修改上面的脚本以输出以下内容:

  • 如果右侧边栏(右侧边栏)有小部件,则显示右侧边栏
  • 如果 Shop Sidebar (shop-sidebar) 有小部件,则显示 Shop Sidebar
  • 如果右侧边栏和商店边栏都有小部件,则显示商店边栏
  • 如果右侧边栏或商店边栏都没有任何小部件,则不要显示任何内容

谢谢。

【问题讨论】:

    标签: php wordpress widget if-statement


    【解决方案1】:
    // 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');
    }
    

    【讨论】:

      【解决方案2】:

      代码应该很简单,因为您已经定义了商店侧边栏优于右侧边栏的良好优先级。如果您不需要显示侧边栏,则无需担心。

      <?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!
      ?>
      

      谢谢。

      【讨论】:

        【解决方案3】:

        functions.php 文件正在修改它。

        【讨论】: