get_post_meta not working
-
Hi. Trying to expand my WordPress theme coding and general php skills. I’ve run into a snag with some code for displaying custom fields in a template.
The chart I want to display has two types of fields, one for a link name, the other for the page link URL . If the link is user-unspecified, I want the page to display just the plaintext name. If the link is specified, I want the name and URL to form the link.
‘dad’ and ‘dad_link’ would be an example of a pair of fields, one containing a name, the other a wordpress page URL. So I feed ‘dad’ to this function:
<?php function islink($key){ $key2 = $key.'_link'; $meta = get_post_meta($post->ID, $key2, true); if($meta != '') echo '<a href="',the_field($key2),'">',the_field($key),'</a>'; else echo the_field($key); }?>
And then..
<?php islink('dad');?>
This should output a named link if the field dad_link is set, or just the dad string, if not. But I can’t get it working.If I use the code
if($meta != '')
then nothing becomes a link.
If I use the codeif($meta != 'null')
then everything becomes a link, regardless of if the link is null or not. The code isnt picking between the instances where the link is set and not set.So there’s something I’m not understanding about get_post_meta. Anyone see what I’m missing?
- The topic ‘get_post_meta not working’ is closed to new replies.