I might be doing this all wrong. I have a custom field called "enable_adsense" which is radio button with 2 choices which are either "yes" or "no"
What I'm trying to do is display something if the value is "yes" and something else if it's "no".
I can't get it to work, here's what I have so far:
<?php
global $post;
$enable_adsense = get_post_meta($post->ID, 'enable_adsense', true);
if ($enable_adsense == 'yes') { ?>
Adsense gode goes here
<?php } else { ?>
Alternale code goes here
<?php } ?>
I used var_dump($enable_adsense);
and it returned string(0) ""
Navjot Singh answers:
From the code it seems you are using it outside the loop. Your code should work fine. Can you try this alternative?
<?php
global $wp_query;
$postid = $wp_query->post->ID;
enable_adsense = get_post_meta($postid, 'enable_adsense', true);
if ($enable_adsense == 'yes') { ?>
Adsense gode goes here
<?php } else { ?>
Alternale code goes here
<?php } ?>
Glenton Samuels comments:
OK that worked, as it turns out my original code was actually correct but for some reason it doesn't work when the Custom Post Type UI plugin is activated. Being that I use Custom Post Type UI extensively it would have taken me all day to manually write all those custom post types and taxonomies in the functions.php file, and then I would have no guarantee it would work.
So I'm glad the $wp_query; alternative method works.
Arnav Joy answers:
Try this
$enable_adsense = get_post_meta(get_the_ID (), 'enable_adsense', true);
Arnav Joy comments:
How you have crratef custom field ?
Glenton Samuels comments:
I used the Advanced Custom Fields plugin to create the radio button metabox, I also disabled the plugin and added the custom field manually, to make sure it wasn't a plugin issue.
Glenton Samuels comments:
I tried your code and it's returning:
string(0) "" Alternale code goes here
Arnav Joy comments:
Can you pass static post id to the function if see it is returning anything
Also share full code where you are calling this.
Eric P. answers:
@Glenton Samuels, your code is working for me, with a few caveats about the behavior of custom fields in WordPress (I'm using version 3.6.1)
If you are trying to see the results in a "preview" mode, the custom fields may not be updating until you "update" the post/page, or at least update the custom field(s). If you have "custom fields" metabox enabled (the default WordPress one), there's an "update" button with each field. If you click that, it will update just that custom field in the database, and your preview will work as expected for the new value. If you don't do that, the "preview" button will show changes to the Post text and title, but not reflect any updates in the custom fields. I'm not sure if that is a feature or a bug, but it definitely <em><strong>is</strong></em>.
Also, if you don't have that as a "required field" in your configuration for that plugin, it won't add the field. If it is a required field, the post_meta entry is not updated until you press the "update" button for the post. Or you can manually add it in the "Custom Fields" metabox (the default one from WordPress) and use the "update" button there to force the update.
Finally, the syntax for the choices on that plugin are a bit wierd. It's {value}_space_:_space_{Display choice} for each choice in the "choices" box. So, if you want to make the choices "yes" and "no" and show "Yes, do AdSense" and "No, skip AdSense" next to the buttons, your choices would be:
yes : Yes, do AdSense
no : No, skip AdSense
I hope this all helps.
Feel free to [[LINK href="http://www.wpquestions.com/user/profile/id/15087"]]message me here if you need[[/LINK]] further assistance.
Hariprasad Vijayan answers:
Hello,
Which page you are trying to achieve this? Can you please show complete code of that page?