Quote:
Originally Posted by asifnkhan
is there any way to add a condition to the widget areas? For instance, If there is nothing in the widget area then it just won't render on the site and if it has something then it will show on the site. Right now, empty or not, it just reserves the space on the site for the widget.
|
This is not for beginners, but with some basic PHP knowledge, it is doable, and there are several ways, but I'll Dick and Jane it as best as I can. For starters, I'd recommend you read this thread and this post.
I made a sample like you described, and randomly made widget 2 empty half the time. In real life, if you were going to use this, you'd probably have functions building the widgets with conditions, which would or could result in an empty widget (otherwise you wouldn't need this technique).
First, in my template, I put:
HTML Code:
<?php dynamic_sidebar( 'footer-widgets' ); ?>
HTML Code:
<?php echo custom_make_footer_widgets(); ?>
PHP Code:
function custom_make_footer_widgets()
{
$fw1 = "<p>Whatever you want in footer widget one</p>";
$fw2 = "<p>Whatever you want in footer widget two</p>";
$fw3 = "<p>Whatever you want in footer widget three</p>";
/* make widget 2 empty half the time for testing */
if (rand(0,1))
$fw2 = "";
/* build array of widgets with something in them */
$fw_array = array();
if ($fw1)
$fw_array[] = $fw1;
if ($fw2)
$fw_array[] = $fw2;
if ($fw3)
$fw_array[] = $fw3;
/* make different layouts for different numbers of widgets */
if (count($fw_array) == 3)
$grid = '<div class = "col4">';
if (count($fw_array) == 2)
$grid = '<div class = "col6">';
if (count($fw_array) == 1)
$grid = '<div class = "col12">';
/* compile html */
$html = '<div class = "row">';
foreach ($fw_array as $fw)
$html .= $grid . $fw . "</div>";
$html .= "</div>";
return $html;
}