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 have recreated it in Wordpress and am working offline. Everything is good and works the way I want it to. I have does this in order to add a blog. To get the image above the content I used custom fields. When the blog page is a static page it works fine but when I set it be the post page the image does not show up. The container for the image shows up, just not the image. Here is the code for the beginning of the single.php and index.php up until the loop starts.

       <?php
       get_sidebar(); ?>

       <?php

        $image = get_post_meta($post->ID, img, true);
        ?>

        <img src="<?php echo $image; ?>" class="img"/>


        </div>

<div id="content" class="narrowcolumn" role="main">

<?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

I have read a few things about calling the custom field outside the loop but I'm not sure how to do this or if that is even correct. Any help would be greatly appreciated.

share|improve this question

1 Answer 1

This is pretty wonky (I would never do it), but it should work if you access the global.

    global $post;
    $image = get_post_meta( $wp_query->post->ID, 'img', true);

    <img src="<?php echo $image; ?>" class="img"/>
share|improve this answer
1  
Can't you use global $post instead of global $wp_query then use get_post_meta ? –  BandonRandon Jul 5 '13 at 8:03

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.