Okay, thanks for that – it helped me accomplish what I was hoping for.
So it now looks like this in “single.php”:
Brand: <?php $key=”Brand”; echo get_post_meta($post->ID, $key, true); ?>
But what it doesn’t say is how to get the $key name to come up as well.
So if there is no brand name available then I still have the word “Brand:” with a blank space after.
What I would like to do is get the name of the ($key) to come before the meta data dynamically, so that if there is no brand entered into the custom field, it would show nothing at all for that string.
Is there a way to do this?
I’m not sure it’s the best way to do it but here is what i use
<?php $key = get_post_meta($post->ID, 'YOURCUSTOMFIELD', true) ; if (!empty($key)) { ?>Brand:<?php echo ($key)} ?>
I copied the code you provided and changed the ‘YOURCUSTOMFIELD’ part to the name of my custom field and returned an error.
Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ in XXXXX/XXXXXX/XXXXX/single.php on line 434
Thanks for trying to help, though. Maybe I did something wrong?
add a semicolon after echo ($key)};
hmmm I’m really bad at troubleshooting php… I’ll post the code I use without any modifications. Maybe it will be more helpfull
<?php $email = get_post_meta($post->ID, 'ccio_email', true) ; if (!empty($email)) { ?><li>Courriel: <?php echo get_post_meta($post->ID, 'ccio_email', true) ; ?></li><?php } ?>
Unfortunately so am I, lol.
Is there a way to get the name of the ($key) to come before the meta data dynamically, so that if there is no brand entered into the custom field, it would show nothing at all for that string?
Try using the get_post_custom() function (Codex ref), which returns an associative ( ‘key’ => ‘value’ ) array of all post metadata. e.g.
$mypostmeta = get_post_custom();
Then, you can do whatever you need to, with your specific meta keys.
That would be perfect but instead of showing the name of the key (i.e. – “Brand”) it shows a numeric value from 0-whatever.
So if the custom field metadata is “Budweiser” it shows “0 => Budweiser”.
I need it to show “Brand => Budweiser”. And so on with any other keys that are specified. Almost there, though. Any idea how to change the number to the actual key name?
Thanks everyone for your help, I do appreciate it!
So if the custom field metadata is “Budweiser” it shows “0 => Budweiser”.
I need it to show “Brand => Budweiser”.
$postmeta = get_post_custom();
$mymetakey = $postmeta['my-meta-key'];
$mymetavalue = $mymetakey[0];
Well I figured this out, so I decided to share just in case anyone else has this issue.
Instead of trying to select which keys I wanted to show, I decided to try to focus on the ones I DIDN’T want to show, and specified that like so (just replace “yourkey” with the name of the key that you do not want to show to visitors, all other keys:values will show.) :
[Code moderated as per the Forum Rules. Please use the pastebin]
I don’t know that this is the best way to do it, but it worked for me. Thanks to all who were kind enough to offer help!