-1

Please help me out.I am battling since three days.

How to fetch value of a custom field to display it in post content area?

shweta
  • 19
  • 1
  • 5
  • Image custom field? where do you storing the file info and image. get_post_meta. can help you to bring the value of custom post meta – Gowri Oct 09 '13 at 10:00
  • Thanks for reply.I added some code in function.php,through which I can see my custom field while am uploading any image and it is retaining those values which I am giving it but what I want is to fetch that custom field value into my post.tried many things but no result. – shweta Oct 09 '13 at 10:04
  • first you didn't answer my questions. Have you tried 'get_post_meta'. can you explain what many things you have tried – Gowri Oct 09 '13 at 10:10

3 Answers3

0

You can get custom field to display using below code:

get_post_meta($post_id, $key, $single);
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • add_filter('attachment_fields_to_edit', 'edit_media_custom_field', 11, 2 ); add_filter('attachment_fields_to_save', 'save_media_custom_field', 11, 2 ); function edit_media_custom_field( $form_fields, $post ) { $form_fields['custom_field'] = array( 'label' => 'Product Code', 'input' => 'text', 'value' => get_post_meta( $post->ID, '_custom_field', true ) ); return $form_fields; } function save_media_custom_field( $post, $attachment ) { update_post_meta( $post['ID'], '_custom_field', $attachment['custom_field'] ); return $post; – shweta Oct 09 '13 at 10:14
  • and I want to fetch it in category.php. I am using echo get_post_meta($post->ID, '_custom_field', true); – shweta Oct 09 '13 at 10:15
  • $key: it will be name of your custom field – Bhumi Shah Oct 09 '13 at 10:18
  • $single :If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. – Bhumi Shah Oct 09 '13 at 10:51
  • but this displays custom field w.r.t to post. I want w.r.t image.Can I? – shweta Oct 09 '13 at 10:53
  • Yes, you can pass attachment id – Bhumi Shah Oct 09 '13 at 10:55
0

Custom field data can be fetched inside template (inside loop) using this functions:

Fetches all fields

<?php the_meta(); ?>

OR for specific value

get_post_meta($post_id, $key, $single);

You can learn more + examples in official wordpress documentation about custom fields.

0

You can use this through below code:

<?php echo get_post_meta($post_id, $key, true); ?>

Here your arguments must same as you have mention in function.php, while creating your custom field like as in:

<?php add_post_meta($post_id, $key, $single,true); ?>

Thanks.

Krunal Shah
  • 2,083
  • 12
  • 27