Thursday, 15 May 2014

php - 'if/elseif' and 'is_active_sidebar' statement in Wordpress theme -



php - 'if/elseif' and 'is_active_sidebar' statement in Wordpress theme -

i have code:

<?php if ( is_active_sidebar('lt') && is_active_sidebar('rt')) { ?> <div class="grid_6 eqh"> <?php } elseif ( !is_active_sidebar('lt') || !is_active_sidebar('rt')) { ?> <div class="grid_9 eqh"> <?php } elseif ( !is_active_sidebar('lt') && !is_active_sidebar('rt')) { ?> <div class="grid_12 eqh"> <?php }; ?>

it's part of wordpress theme i'm developing. it's supposed show different classes according sidebar beingness active or not. response wp if sidebar active 1 , null if isn't. works if 'lt' sidebar active, 'rt' not, diplays 'grid_9' div, if none of sidebars active, displays 'grid_12' div, if 'rt' sidebar inactive , 'lt' active, still displays 'grid_6' div. if remove 1st if statement:

<?php if ( is_active_sidebar('lt') && is_active_sidebar('rt')) { ?>

it doesn't display of divs.

i've tried every combination came mind, i've separated the:

<?php } elseif ( !is_active_sidebar('lt') || !is_active_sidebar('rt')) { ?>

statement in 2 different ones, tried separate if statements without 'elseif', tried with, 'and' , 'or' instead of '&&' , '||' , whole bunch of other variations, nil worked.

if can help me or @ to the lowest degree pont me in right direction, i'd grateful.

as per @one-trick-pony 's suggestion, code now:

<?php $ltactive = is_active_sidebar('lt'); $rtactive = is_active_sidebar('rt'); if($ltactive && $rtactive){ $class = 'grid_6'; }elseif(!$ltactive && !$rtactive){ $class = 'grid_12'; }else{ $class = 'grid_9'; // <== } ?> <div class="<?php echo $class; ?> eqh">

the php code working, seems there bug is_active_sidebar() function in wp, ignoring question on wp back upwards forum.

first, set homecoming result in variables (it speed processing little):

$ltactive = is_active_sidebar('lt'); $rtactive = is_active_sidebar('rt');

now, problem in 2nd status - you're checking if of sidebars inactive, , if 1 of them or both are, 3rd status never evaluated.

so:

(!$ltactive && !$rtactive)

must go before:

(!$ltactive || !$rtactive)

(last status not necessary, can leave else{...})

ok here's code:

if($ltactive && $rtactive){ $class = 'class when both sidebars active'; }else(!$ltactive && !$rtactive){ $class = 'class when both sidebars inactive'; }else{ $class = 'class when 1 of sidebars active (either one)'; } ?> <div class="<?= $class; ?>">

php wordpress if-statement

No comments:

Post a Comment