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 a page where I'm pulling in content from another post, outside of the loop.

I've been successfull at getting the content of the specified post, but I cannot get the image from a custom field (advanced custom image field).

This is working to get the content from the post who's ID is 299:

$my_id = 299;
$post_id_299 = get_post($my_id);

$content_299 = $post_id_299->post_content;
$content_299 = apply_filters('the_content', $content_299);
$content_299 = str_replace(']]>', ']]>', $content_299);
$content_299_chars = 238; 
$modcontent_299 = substr($content_299, 0, $content_299_chars);

<p><?php echo $modcontent_299; ?></p>

This is what I'm after to get the image from the post who's ID is 299 (if it exists):

$attachment_id = get_field('image', 299);
$size = 'full';
$image = wp_get_attachment_image_src( $attachment_id, $size ); 

if( get_field('image') ):
   echo '<img src="' . $image[0] . '" alt="' . the_title() .'" width="238" height="158" />';
endif; 

I'm sure I need to change some of these variables to get the image field from the specific post id, similar to how it's done to get the content, but I'm confused at this point as to how to do that for a custom field...

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

see documentation for get_field. change:

$attachment_id = get_field('image');

to pass the post ID as second argument:

$attachment_id = get_field('image', 299);

or:

$attachment_id = get_field('image', $my_id);
share|improve this answer
    
Thanks for replying Milo, unfortunately that doesn't work - nothing returns. –  ByteMyPixel Dec 19 '12 at 21:00
    
is your field set to return the image ID, and not an image object or array of some kind? –  Milo Dec 19 '12 at 21:01
    
I see - the remaining problem was in the conditional, I needed to also update if( get_field('image') ): with if( get_field('image', 299) ): - that was it! Thank you for your help! –  ByteMyPixel Dec 19 '12 at 21:07
add comment

i think your using your code to show title and alt on post image then try it.

    $post_title  = get_the_title($my_id);

    echo get_the_post_thumbnail($my_id,
        'full', 
        array(
            'alt'   => $post_title, 
            'title' => $post_title,
            'class' => 'my_post_img_class'
        )
    );

it will echo

<img src="your_image_path.jpg" alt="post_title" title="post_title" class="my_post_img_class" />

or use this below function to get custom post field get_post_field('post_content', $my_id);

share|improve this answer
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.