To get a custom field, the code is
$custom_field_value = get_post_meta($post->ID, 'custom_field_name', true);
It will save the value stored in your custom field on to $custom_field_value, then you can display, like this:
echo $custom_field_value;
There is a very simple method, but are a lot of ways more, take a look on codex: custom fields it can really help you.
Thank you for reply aritzalvarez
I pasted the following code in the PHP code widget on the side bar and set the custom field title same as post ID, but nothing shows up on sidebar on post apart from just the title. could you please assist if there is some thing missing in code or i’m doing some thing wrong.
<?php
$custom_field_value = get_post_meta($post->ID, 'custom_field_name',true);
echo $custom_field_value;
?>
same post ID?
So… I guess you meant same custom field name; the $post-ID isn’t to edit, it’s to know what post is being read….
Ok, to it works you need have it inside the loop. If your theme hace it outside the loop it will not work…
<?php
if ( have_posts() ) { while ( have_posts() ) { the_post();
// HERE YOU CAN USE IT
} // end while
} // end if
?>
A way to fix it is use
<?php
$custom_field_value = get_post_meta($post->ID, 'custom_field_name',true);
?>
Inside of the loop and, then, use storage value and
echo $custom_field_value;
In the sidebar… But only will work if your sidebar is after the loop….
Ypu can try add a secondary loop in the sidebar, but it’s a pretty messy way to do it
I will try to use it in the loop, where the content is shown, to see if everything is ok
Thank you for the update aritzalvarez, really appreciate your effort
I’m fairly new to WordPress and PHP, i’m having difficulty in understanding where to place the above code. should i add the first part of code to funtions.php or put the code in the php code widget side bar ?