0

I have the following code that works but doesn't look good:

<?php 
    $col = 'one';

    if( is_active_sidebar('footer') && is_active_sidebar('footer-2') && is_active_sidebar('footer-3') ) :
        $col = 'three';
    elseif( ( !is_active_sidebar('footer') && is_active_sidebar('footer-2') && is_active_sidebar('footer-3') ) || ( is_active_sidebar('footer') && !is_active_sidebar('footer-2') && is_active_sidebar('footer-3') ) || ( is_active_sidebar('footer') && is_active_sidebar('footer-2') && !is_active_sidebar('footer-3') )  ) :
        $col = 'two';
    elseif( !is_active_sidebar('footer') && !is_active_sidebar('footer-2') && is_active_sidebar('footer-3') || ( is_active_sidebar('footer') && !is_active_sidebar('footer-2') && !is_active_sidebar('footer-3') ) || ( !is_active_sidebar('footer') && is_active_sidebar('footer-2') && !is_active_sidebar('footer-3') )  ) :
        $col = 'one';
    endif; ?>

    <div class="<?php echo $col . '-column'; ?>">

        <?php if( is_active_sidebar('footer') ) : ?><div class="footer-widgets footer-widgets-1"><?php dynamic_sidebar('footer'); ?></div><?php endif; ?>
        <?php if( is_active_sidebar('footer-2') ) : ?><div class="footer-widgets footer-widgets-2"><?php dynamic_sidebar('footer-2'); ?></div><?php endif; ?>
        <?php if( is_active_sidebar('footer-3') ) : ?><div class="footer-widgets footer-widgets-3"><?php dynamic_sidebar('footer-3'); ?></div><?php endif; ?>

    </div>

Is there a better way to create the same logic? Any ideas? Thanks

1
  • That question is really a CSS question and not a conditional logic question. I want to be able to style these columns based on how many are active. I'm sure I can do this with JS and my code works. I was looking for a better solution (if possible). Thanks.
    – katart
    May 27 '15 at 18:57
0

If I am reading this right, all you need is a count of the active sidebars, so:

$col = 0;
if(is_active_sidebar('footer')) {
  $col++;
} 
if(is_active_sidebar('footer-2')) {
  $col++;
} 
if( is_active_sidebar('footer-3')) {
  $col++;
}?>

<div class="<?php echo 'column-'.$col; ?>">

Note: I flipped the "column" class so that it begins with a letter. Numbers are not allowed as the first character. If you wanted to convert that to "one", "two", etc you should be able to cook that up yourself without much effort but it doesn't seem worth the overhead to me when a simple structural change makes it unnecessary.

7
  • Thanks for this but this will not work since I assume this counts how many widgets are active within each sidebar. All I need to do is check if the sidebar has at least one active widget.
    – katart
    May 27 '15 at 18:53
  • Why do you assume that? is_active_sidebar() checks whether the sidebar is in use. It doesn't count widgets but I assumed you knew that since you were already using the function. codex.wordpress.org/Function_Reference/is_active_sidebar
    – s_ha_dum
    May 27 '15 at 19:05
  • I'm sorry. I understand how this function works; I only assumed that since this code resulted in '1' even if I had 2 active sidebars.
    – katart
    May 27 '15 at 19:40
  • Oops. Sorry. My mistake. See the edit.
    – s_ha_dum
    May 27 '15 at 19:47
  • Perfect! Although I'm not exactly sure how (or why) the integer is being iterated through with this logic. Do you have time to explain?
    – katart
    May 27 '15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.