带有三个 && 条件的 PHP IF 语句在 Wordpress 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4401882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP IF Statement with three && conditions not working in Wordpress
提问by lisovaccaro
<?php
if (!is_front_page()) && (!is_single()) && (!is_page())
echo "<a href='http://chusmix.com/'>Cambiar Imagen</a>";
?>
It's actually the elseif of a bigger if but I tried to do separately trying to increase my chances of making it work. The bigger statement is this one, all works except the elseif:
它实际上是一个更大的 if 的 elseif,但我试图单独做,以增加我让它工作的机会。更大的声明是这个,除了 elseif 之外的所有工作:
<?php
$res= get_search_query();
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
echo "<a style='padding-left:180px;' href='http://chusmix.com/'>Cambiar Imagen</a>";
echo "<hr style='border: 0;'>";
}
elseif (!is_front_page()) && (!is_single()) && (!is_page())
echo "<a href='http://chusmix.com/'>Cambiar Imagen</a>";
?>
回答by John Carter
You need to have a matched pair of brackets around the whole if statement.
您需要在整个 if 语句周围有一对匹配的括号。
So either add an extra bracket at the start and end, or remove some of the unneeded ones like this:
因此,要么在开头和结尾添加一个额外的括号,要么删除一些不需要的括号,如下所示:
<?php
if (!is_front_page() && !is_single() && !is_page())
echo "<a href='http://chusmix.com/'>Cambiar Imagen</a>";
?>
回答by BoltClock
You're missing an outer set of parentheses:
您缺少一组外部括号:
elseif ((!is_front_page()) && (!is_single()) && (!is_page()))
You can leave out each individual pair that surrounds the function names though to make it look cleaner:
您可以省略围绕函数名称的每一对,但使其看起来更简洁:
elseif (!is_front_page() && !is_single() && !is_page())
回答by jwueller
It should look like this:
它应该是这样的:
if (!is_front_page() && !is_single() && !is_page())
echo "<a href='http://chusmix.com/'>Cambiar Imagen</a>";
You do not need a pair of parenthesis around each expression, but you need to wrap your condition.
您不需要在每个表达式周围使用一对括号,但您需要包装您的条件。
回答by The Surrican
<?php
$res= get_search_query();
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
echo "<a style='padding-left:180px;' href='http://chusmix.com/'>Cambiar Imagen</a>";
echo "<hr style='border: 0;'>";
}
elseif ((!is_front_page()) && (!is_single()) && (!is_page())) {
echo "<a href='http://chusmix.com/'>Cambiar Imagen</a>";
}
?>
there are brackets missing to group the conditions conjunctive...
缺少括号将条件组合在一起......
回答by Jason Plank
I think you need an enclosing set of parenthesis around all three conditions:
我认为您需要在所有三个条件周围加上一组括号:
if ((!is_front_page()) && (!is_single()) && (!is_page()))