Should be like this:
<?php
if ($values == get_post_custom_values('description'))
echo $values[0];
else
echo 'no description';
?>
Notes:
1. Need to use two equal signs in the condition because you’re comparing the two.
2. It’s best practice to use single quotes for strings as it doesn’t match with double quote which are used in HTML. However, you’ll need to use double quotes for escape characters such as \n, \r and \t or else it won’t work.
3. If the IF statement contains more than one line, you’ll need curly brackets like this:
<?php
if ($values == get_post_custom_values('description')) {
echo $values[0];
// more than one line
} else {
echo 'no description';
// more than one line
}
?>
Where are you placing the code?
PHP Conditions need parenthesis..(
if( something ) {}
A single equals assigns a value to something, such as a variable.
$a = 2; // $a is now defined with a value of 2
A double equals or exclamation preceding an equals is a comparison..
if( $a == 2 ) {
// If $a is equal to 2
}
if( $a != 2 ) {
// If $a is not equal to 2
}
If you only want the first meta value back, use get_post_meta().. in place of get_post_custom_values and you’ll get a single value back, the first one (that avoids the needs to reference the first item in the array like you’re currently doing, ie. $values[0]).
http://codex.ww.wp.xz.cn/Function_Reference/get_post_meta
I am using it in the index.php for post content. What I originally had that worked was:
<?php $values = get_post_custom_values("introduction"); echo $values[0]; ?>
But I wanted to amp it up a little and return a message if no custom value was defined for that post. So if I don’t have an introduction, it would return a defined message.
The one Joseph gave me didn’t work. When I didn’t post anything for that value it was blank and when I posted something in that value it returned the message it should of shown if no value was defined. So it was reversed if that makes sense.
Try this?
<?php
$my_values = get_post_meta( $post->ID, 'introduction', true );
if( $my_values ) {
echo $my_values;
}
else {
echo 'Nothing found';
}
?>
Q: This code is inside the loop right?
That worked really well, now only one more thing. I am trying to get that code to work with the following:
<?php
$my_values = get_post_meta( $post->ID, 'trailer', true );
if( $my_values ) {
echo '<div align="center"><object width="490" height="220"><param name="movie" value="http://www.youtube-nocookie.com/v/$my_values?fs=1&hl=en_US&rel=0&color1=0x3a3a3a&color2=0x999999"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/$my_values?fs=1&hl=en_US&rel=0&color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="490" height="220"></embed></object></div>';
} else {
echo 'Sorry, there is no cinema trailer yet.';
}
?>
Is the something I am doing wrong? I would assume a value inside a value wont work.
Variables are treated as literal values inside a single quote string..
Change this..
/$my_values?
..for..
/' . $my_values . '?
And it should fix your problem… π
That worked perfect, thank you very much for your time sir. It is greatly appreciated. π
Glad to hear it’s working for you.. and happy to help.. π