0

What I'm simply trying to do is grab the featured image thumbnail image from a specified post/page and display that image on any other page somewhere.

So for instance, if it's a single post, use the thumbnail from post 9. If it's this page, use the thumbnail from 82, and so on.

It doesn't seem as simple as this:

$thumbnail_id = get_the_post_thumbnail(82);

Because I've tried that and it doesn't work in all scenarios.

Is there a universal way to do this?

1
  • What are you expecting the above code to do, exactly? You are loading the thumbnail's HTML into the $thumbnail_id variable, but not echoing it.
    – vancoder
    Commented Apr 3, 2013 at 23:05

3 Answers 3

1

Each post object has a public member $post_parent. That is the post ID of the parent post. So this should work:

$thumbnail = '';

if ( ( 'event_posts' == get_post_type()
        || is_page( 'events' )
        || is_page( 'restaurants' )
        || is_page( 'recreation' )
        || is_page( 'entertainment' )
        )
    && ! empty ( $GLOBALS['post']->post_parent )
    )
{
    $thumbnail = get_the_post_thumbnail( $GLOBALS['post']->post_parent );
}

echo $thumbnail;
1
  • I tried your suggestion but couldn't get it to work out. I also revised my question. Commented Apr 9, 2013 at 1:21
0

method 1)

<?php
$target_post_id = '999';
$feat_image = wp_get_attachment_url(get_post_thumbnail_id($target_post_id));
echo '$feat_image';
?>

method 2)

<?php 
$target_post_id = '999';
if (has_post_thumbnail($target_post_id) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id($target_post_id), 'single-post-thumbnail' ); ?>
<?php echo $image[0]; ?>
<?php endif; ?>
1
  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Apr 9, 2013 at 7:19
0

Looks like this might be the reason why it wasn't working the way I needed.

before -> if ( has_post_thumbnail () )

Seems that only relates to the specific post/page you're on, and not the page/page that you're getting the image from - if you don't enter a post ID. But then I tried it with the post ID I was trying to retrieve from and it works.

after -> if ( has_post_thumbnail (9) )

1
  • if you look at the function in codex, you'll see that you can pass the post id of the post you want to check. also, you're missing the parens on the function: if ( has_post_thumbnail() ).
    – Milo
    Commented Apr 9, 2013 at 1:48

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.