if else语句,如果没有输出else?

时间:2015-04-14 10:07:27

标签: php wordpress if-statement

我有一个小的if else语句,其中if得到输出,但不是当前的else?我试过双管,但没有运气。

我的页面顶部有一个变量 -

$portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );

然后我的if else不起作用 -

<?php if ( $portfolio_style == 'portfolio_style_one' || 'portfolio_style_two' ) { ?>
    <div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
    <div class="wfull">
<?php } ?>

如果选择了一个或两个投资组合风格,我想要它,显示类'grid',否则如果选择了样式三,则显示类'wfull'

我还可以完善这段代码吗? -

 $portfolio_style = ( isset( $uno_theme['portfolio-style-select'] ) ? $uno_theme['portfolio-style-select'] : null );

非常感谢

4 个答案:

答案 0 :(得分:1)

您不能在$portfolio_style之后省略||变量,否则唯一的字符串'portfolio_style_two'将始终评估为true,如描述conversion to boolean的手册中所述:< / p>

您的代码应为:

<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>
    <div class="grid">
<?php } elseif ( $portfolio_style == 'portfolio_style_three' ) { ?>
    <div class="wfull">
<?php } ?>

答案 1 :(得分:0)

尝试更改if条件:

<?php if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' ) { ?>

答案 2 :(得分:0)

您需要将此行更改为

if ( $portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two' )

答案 3 :(得分:0)

试试这个:

echo ($portfolio_style == 'portfolio_style_one' || $portfolio_style == 'portfolio_style_two') ? '<div class="grid">'  : (($portfolio_style == 'portfolio_style_three' ) ? '<div class="wfull">' : '');