PHP IF Statement with three && conditions not working in WordPress

<?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:

<?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>";
?>

Related posts

Leave a Reply

5 comments

  1. 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())
    
  2. You need to have a matched pair of brackets around the whole if statement.

    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>";
    ?>
    
  3. 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.

  4. <?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…