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.
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.
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.
Thanks.
It is being modified by the
functions.php
file.