Here’s some code I use to display two custom fields in a sidebar. Replace MyField1 & MyField2 with your custom field names.
<?php // display custom fields
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'MyField1', true);
echo get_post_meta($postid, 'MyField2', true);
wp_reset_query();
?>
Function Reference/get post meta
Thank you so much!
I tried like 10 different “solutions”, non worked. But yours did. Do you know if there is a way to combine your code with a UL? Like wrap it in a UL or something?
You’re welcome.
I tried like 10 different “solutions”, non worked. But yours did.
That may be because most examples are meant to work only within the loop, whereas this is one specifically meant to work outside the loop, which is where the sidebar is.
Do you know if there is a way to combine your code with a UL? Like wrap it in a UL or something?
Sure – you can add whatever other HTML you want to the code, for example here’s an opening ul & li:
echo "<ul><li>";
To “concatenate” code (put two bits together) you’re echoing, use a dot in between the elements.
Example:
echo "<ul><li>" . get_post_meta($postid, 'MyField1', true);
You can probably figure out the rest. 🙂
Ref: http://phphowto.blogspot.com/2006/12/concatenate-strings.html