PHP If/Else for WordPress Custom Fields
In PHP we have the following conditional statements: statement — executes some code if one condition is true statement — executes some code if a condition is true and another code if that condition is false statement — executes different codes for more than two conditions statement — selects one of many blocks of code to be executed PHP — The if Statement The statement executes some code if one condition is true. Syntax if ( condition ) < code to be executed if this condition is true; >elseif ( condition ) < code to be executed if first condition is false and this condition is true; >else < code to be executed if all conditions are false; >Example Output «Have a good morning!»
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
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
Some html code
Some html code
the syntax for php is as follows:
You only need the opening and closing php tags () 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 "";
which will output the following html:
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:
<?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); >?>
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 Else, The PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this statement. Syntax if (condition1) < //code to be executed if condition1 is true >elseif (condition2) < //code to be executed if condition2 is true >elseif (condition3)
PHP If/Else for WordPress Custom Fields
I have a site that is using custom fields, and I want to either show the content for the field if the field is being used, and if the field is not being used per a Post then of course don’t show the contents of the fields.
Seems my code below is not correct, as it is showing the content in the else block when the Post is not using any of the custom fields.
Any help would be greatly appreciated!
Here is the Post in question: http://beta.ohsinsider.com/insider-top-stories/workers%E2%80%99-compensation-may-not-shield-you-from-lawsuits-by-injured-workers
Here is the Post Edit showing that the field I am calling in my code are not being used (http://screencast.com/t/aBjt1drIw).
I have confirmed that when I do input the value for the custom field is it being outputted in the Post.
Here is the code I am using:
ID, 'pdf', true); $wordurl = get_post_meta($post->ID, 'word', true); if( !empty($pdf) || !empty($word) ) < ?> else < ?> ID, 'pdf', true); ?>"> ID, 'word', true); ?>"> ?>
Your code block is a little confusing . partly because your logic fails to take into account what would happen if the post has a PDF custom field and not a Word custom field . you’ll still be displaying both sets of markup. Instead, I would recommend this:
ID, 'pdf', true) && get_post_meta($post->ID, 'word', true) ) : ?>ID, 'pdf', true); ?>">ID, 'word', true); ?>">
This checks for both custom fields and, if either one is missing, it skips rendering the markup and «shows nothing.» If both are there, it renders your markup.
Inverting the logic of the first statement would be the ideal solution.
ID, 'pdf', true); $wordurl = get_post_meta($post->ID, 'word', true); if( empty($pdf) && empty($word) ) < ?> ID, 'pdf', true); ?>"> ID, 'word', true); ?>"> ?>
Note although this is inverted logic, it doesn’t appear to be correct. As this would display download links if both were empty. The following should be your if statement.
ID, 'pdf', true); ?>"> ID, 'word', true); ?>"> else <> ?>
Else if in php Code Example, follow. grepper; search snippets; faq; usage docs ; install grepper; log in; signup
PHP if. else. elseif Statements
conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use Conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement — executes some code if one condition is true
- if. else statement — executes some code if a condition is true and another code if that condition is false
- if. elseif. else statement — executes different codes for more than two conditions
- switch statement — selects one of many blocks of code to be executed
PHP — The if Statement
The if statement executes some code if one condition is true.
Syntax
Example
Output «Have a good day!» if the current time (HOUR) is less than 20:
PHP — The if. else Statement
The if. else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if ( condition ) <
code to be executed if condition is true;
> else <
code to be executed if condition is false;
>
Example
Output «Have a good day!» if the current time is less than 20, and «Have a good night!» otherwise:
if ($t < "20") <
echo «Have a good day!»;
> else <
echo «Have a good night!»;
>
?>
PHP — The if. elseif. else Statement
The if. elseif. else statement executes different codes for more than two conditions.
Syntax
if ( condition ) <
code to be executed if this condition is true;
> elseif ( condition ) <
code to be executed if first condition is false and this condition is true;
> else <
code to be executed if all conditions are false;
>
Example
Output «Have a good morning!» if the current time is less than 10, and «Have a good day!» if the current time is less than 20. Otherwise it will output «Have a good night!»:
if ($t < "10") <
echo «Have a good morning!»;
> elseif ($t < "20") <
echo «Have a good day!»;
> else <
echo «Have a good night!»;
>
?>
PHP — The switch Statement
The switch statement will be explained in the next chapter.
PHP Exercises
PHP if statement, PHP: Tips of the Day. Change the maximum upload file size. You need to set the value of upload_max_filesize and post_max_size in your php.ini :; Maximum allowed size for uploaded files.