IF, ELSE and ELSEIF Statements

I’m trying to output a sidebar in WordPress depending on whether it has widgets (is active) or not and to display them on my Shop page accordingly.

I’m pretty unfamiliar with PHP, and so far have written up the following scripts to try and make this work, but it doesn’t seem to be happening.

Read More

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

How would I modify the script above to output the following:

  • if the Right Sidebar (right-sidebar) has widgets, display the Right Sidebar
  • if the Shop Sidebar (shop-sidebar) has widgets, display the Shop Sidebar
  • if the Right Sidebar and Shop Sidebar both have widgets, display the Shop Sidebar
  • if neither the Right Sidebar or the Shop Sidebar have any widgets, don’t display anything

Thank you.

Related posts

Leave a Reply

3 comments

  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. Code should be simple, because you have defined good precedence of Shop sidebar over right sidebar. You don’t need to worry if you don’t need to display the sidebars.

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

    Thanks.