Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use code to show custom field outside the loop in wordpress attachment page (image.php) it's returns nothing.

this is the code i'm used to show the custom field:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'my-custom-field', true);
?>

and i'm trying to show parent post id in attachment page too with get_the_id and use this code

<?php global $wp_query;
$this_page_id = $wp_query->post_parent;
$post_id = get_the_id($post->ID);
echo $post_id ;?>

but the code above just shown the attachment ID not the parent post ID. anyone can help, please ?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

You already have $post object on image.php. Don't complicate and use following:

<?php
echo get_post_meta($post->ID, 'my-custom-field', true);
echo $post->post_parent;
share|improve this answer
    
work well like i wanted, thnks Max –  Advenda Feb 14 '13 at 9:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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