0

I'm currently using a custom field called "Developing" to show whether or not a news story is a developing story. The custom field has a couple of values: a blank option (meaning it is not developing), "Yes," and "Was Developing." This is the code I use to display the word "Developing" in front of a news story title:

<?php if (get_meta('developing') != "") { ?>Developing: <?php } ?>

While it shows "Developing" when I've hit Yes, it also shows "Developing" when I change the option to "Was Developing." I'm assuming this is because I'm calling up the entire thing and not distinguishing between its options in the coding. Does anyone know if I can even do this? For example, can you have different messages show for each option? Something like using "developing's yes option means show this" instead of just "developing means this" like above?

Thank you!

0

2 Answers 2

1

use get_post_meta($post->ID, 'your key', true); instead of get_meta(); compare values like

$developing = get_post_meta($post->ID, 'developing', true);

if ($developing == 'value') {
  // value is matched
} elseif ($developing == 'value2') {
  // value 2 is matched
} else {
  // not matched
}
4
  • +1, but you should Yoda-case the conditionals - i.e. 'value' == $developing, instead of $developing == 'value'. Commented Aug 23, 2013 at 19:36
  • @ChipBennett thanks but it's my style and i think there is nothing anything wrong to compare values, may be you know better. Commented Aug 23, 2013 at 20:46
  • There's actually a very good reason. If you make a syntax error, and write $developing = 'value' instead of $developing == 'value', then the value of the variable is set instead of compares, and the conditional always returns true. If Yoda-cased, then 'value' = $developing will return a syntax error, instead of setting the variable and returning always true. Commented Aug 23, 2013 at 21:55
  • @ChipBennett well i am totally agree and i think i need to change my style, thanks. Commented Aug 24, 2013 at 6:52
0

You have two problems:

  1. WordPress has no get_meta() function; if you're using get_post_meta(), then see @Anjum's answer.
  2. Your conditional is logically incorrect: get_meta('developing') != "". You're saying if the value is anything other than an empty string, then do something. Both developing and was developing are non-empty strings, and will cause that conditional to return true. Instead, use if ( 'Developing' == get_post_meta( ... ) ) {

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.