I use this code to show custom field value outside loop on post page (single.php):

<?php echo get_custom_field('my_custom_field'); ?>

But, that is not working on attachment page (image.php). How to show post custom field on attachment page (image.php template)?

1 Answer 1


If this is related to your other question then the custom field is attached to the post and not the attachment. You need to get the post ID of the post first then you can get the custom field. This should work.

global $wp_query;
$attachment_id = $wp_query->post->ID;
$parent_id = get_post_field('post_parent', $attachment_id);

echo get_post_meta($parent_id, 'my_custom_field', true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.