How to use If, If Else and else in Wordpress How to use If, If Else and else in Wordpress wordpress wordpress

How to use If, If Else and else in Wordpress


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


the syntax for php is as follows:

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

<?phpif(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);}?>


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(); ?>" />