How to use If, If Else and else in Wordpress

62,138

Solution 1

An alternative would be:

<?php if ($condition) : ?>
   <p> Some html code </p> <!-- or -->
   <?php echo "some php code"; ?>
<?php else : ?>
    <p> Some html code </p> <!-- or -->
   <?php echo "some php code"; ?>
<?php endif;  ?>

Solution 2

the syntax for php is as follows:

<?php
if(statement that must be true)
{
    (code to be executed when the "if" statement is true);
}
else
{
    (code to be executed when the "if" statement is not true);
}
?>

You only need the opening and closing php tags (<?php ... ?>) once; one before your php code and the other after. You also need to use "echo" or "print" statements. These tell your program to output the html code that will be read by your browser. The syntax for echo is as follows:

echo "<img src='some image' alt='alt' />";

which will output the following html:

<img src='some image' alt='alt' />

You should get a book about php. www.php.net is also a very good resource. Here are links to the manual pages about if, echo, and print statements:
http://us.php.net/manual/en/control-structures.if.php
http://us.php.net/manual/en/function.echo.php
http://us.php.net/manual/en/function.print.php

edit: You can also use "elseif" to give a new condition that must be met for the next section of code. For example:

&lt;?php
if(condition 1)
{
    (code to be executed if condition 1 is true);
}
elseif(condition 2)
{
    (code to be executed if condition 1 is false and condition 2 is true);
}
else
{
    (code to be executed if neither condition is true);
}
?&gt;

Solution 3

Refer ElseIf/Else If.

But looks like a) you're having trouble with mixing PHP and HTML incorrectly, and b) you're not sure of logic to test if post image exists (can't help with that, sorry). Try this:

<?php 
if ( has_post_thumbnail() ) :
    the_post_thumbnail();
elseif ( /*Some logic here to test if your image exists*/ ): ?>
    <img src="<?php echo catch_that_image() ?>" width="64" height="64" alt="<?php the_title(); ?>" />
<?php else: ?>
    <img src="http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png" width="64" height="64" alt="<?php the_title(); ?>" />
Share:
62,138
rahul sharma
Author by

rahul sharma

Updated on July 21, 2022

Comments

  • rahul sharma
    rahul sharma almost 2 years

    I want to use If, If else and else on wordpress homepage, i am using following condition

    <?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    }?>
    <?php if else { 
    <img src="<?php echo catch_that_image() ?>" width="64" height="64" alt="<?php the_title(); ?>" />
    } else {
    <img src="http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png" width="64" height="64" alt="<?php the_title(); ?>" />}
    ?>
    <?php  ?>
    

    I want to show thumbnail first on homepage, if thumbnail is not available then it must use first image from post as thumbnail and if there is no image in post then it use this image

    http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png
    

    can you please tell me where i am wrong because the above code is not working