Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use If, If Else and else in Wordpress

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

like image 969
rahul sharma Avatar asked Aug 26 '12 14:08

rahul sharma


Video Answer


2 Answers

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;  ?>
like image 99
Oliamster Avatar answered Sep 28 '22 15:09

Oliamster


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;
like image 42
Adam11 Avatar answered Sep 28 '22 16:09

Adam11