简体   繁体   English

Wordpress主题中的“ if / elseif”和“ is_active_sidebar”语句

[英]'if/elseif' and 'is_active_sidebar' statement in Wordpress theme

I have this 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 a wordpress theme I'm developing. 这是我正在开发的wordpress主题的一部分。 It's supposed to show different classes according to a sidebar being active or not. 根据侧边栏是否处于活动状态,应该显示不同的类。 The response I get from wp if a sidebar is active is 1 and NULL if it isn't. 如果侧边栏处于活动状态,我从wp得到的响应为1,否则为NULL。 It works if the 'lt' sidebar is active, but the 'rt' is not, it diplays the 'grid_9' div, if none of the sidebars is active, it displays the 'grid_12' div, but if the 'rt' sidebar is inactive and the 'lt' is active, it still displays the 'grid_6' div. 如果'lt'侧边栏处于活动状态,则起作用,但'rt'则不起作用,它会显示'grid_9'div,如果没有一个侧边栏处于活动状态,它将显示'grid_12'div,但是如果'rt'侧边栏处于非活动状态且'lt'已处于活动状态,它仍显示'grid_6'div。 If I remove the 1st if statement: 如果我删除第一个if语句:

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

it doesn't display any of the divs. 它不显示任何div。

I've tried every combination that came to my 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' and 'OR' instead of '&&' and '||' 用2个不同的语句,尝试不带'elseif'的if语句,尝试用'AND'和'OR'代替'&&'和'|| and a whole bunch of other variations, but nothing worked. 以及其他许多变体,但没有任何效果。

If anyone can help me or at least pont me in the right direction, I'd be very grateful. 如果有人能帮助我或至少向正确的方向迈进,我将非常感激。

As per @one-trick-pony 's suggestion, code is now: 根据@ one-trick-pony的建议,代码现在为:

<?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 is working, but it seems there is a bug with the is_active_sidebar() function in WP, but they are ignoring my question on the WP support forum. php代码正在运行,但是WP中的is_active_sidebar()函数似乎存在错误,但是他们在WP支持论坛上忽略了我的问题。

First, put the return result in variables (it will speed up processing a little): 首先,将返回结果放入变量中(这将加快处理速度):

$ltActive = is_active_sidebar('lt');
$rtActive = is_active_sidebar('rt');

Now, the problem is in your 2nd condition - you're checking if any of the sidebars are inactive, and if one of them or both are, the 3rd condition will never be evaluated. 现在,问题出在您的第二个条件下-您正在检查是否有任何侧边栏处于非活动状态,并且如果其中一个或两个都处于非活动状态,则将永远不会评估第三个条件。

So: 所以:

(!$ltActive && !$rtActive)

must go before: 必须去之前:

(!$ltActive || !$rtActive)

(last condition will not be necessary, you can just leave else{...} ) (不需要最后的条件,您可以离开else{...}


Ok here's the code: 好的,这是代码:

if($ltActive && $rtActive){
  $class = 'class when both sidebars are active';

}else(!$ltActive && !$rtActive){
  $class = 'class when both sidebars are inactive';

}else{
  $class = 'class when only one of the sidebars is active (either one)';      
}

?>
<div class="<?= $class; ?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM