10

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

8
  • 1. }else if (CONDITION) { ... }else{ ... } 2. missing closing } bracket at end Commented Aug 26, 2012 at 14:21
  • cant get you, can you plz tell me using above code what you wanna tell, thnkx Commented Aug 26, 2012 at 14:33
  • 1
    to be honest i think you should start from tutorial :) Commented Aug 26, 2012 at 14:36
  • @knoell8504 has gave you links to proper man pages, it's good start Commented Aug 26, 2012 at 14:39
  • 1
    I agree with @PeterSzymkowski, you should first learn programming and PHP basics. There's some fundamental missing concepts in your code that can't be "fixed" without that understanding. Commented Aug 26, 2012 at 14:39

4 Answers 4

21

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;  ?>
Sign up to request clarification or add additional context in comments.

Comments

6

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;

2 Comments

What if both IF and ELSE are not true?
There is no condition for ELSE. The "ELSE" code executes if the other conditions are not true. In other words, "else" means "if not" to the previous condition.
2

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

Comments

0
//Doing this for over 20 options. 
//Which one is better? The first option will make 20 database calls.
//Only those filters will be invoked for whom the settings are enabled.



// ==A==
if(get_option('some_setting')=='yes')
{
    add_action( 'woocommerce_checkout_process', 'abc_xyz' );
}

function abc_xyz() {


            //Do Something here

}

//=========================OR=========================
//==B==

function abc_xyz() {

        if(get_option('some_setting')=='yes')
        {

            //Do Something here
        }
}
add_action( 'woocommerce_checkout_process', 'abc_xyz' );

1 Comment

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.