• Resolved Drding

    (@drding)


    Is there a quick short amount of code to display a specific post’s featured image using its id?

    Or grab that posts featured image, extract the image’s url and then input it into a img tag?

    For the life of me I can’t get anything to work. I do have thumbnails enabled in my functions by the way.

    something like:

    <?php
    $post = get_post(10);
    $thumbnail = $post->get_the_post_thumbnail();
    ?>
    <div id="image1"><?php echo $thumbnail; ?></div>

    or

    <?php
    $post_id = 10;
    $thumb = get_the_post_thumbnail($post_id);
    echo '<div id="image1">'.$thumb.'</div>';
    ?>

    Something along those lines. I did come across wp_get_attachment_url but I’m not sure how to implement it in my case.

Viewing 2 replies - 1 through 2 (of 2 total)
  • your second example should work; http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
    have you tried that, and what was the result?

    Thread Starter Drding

    (@drding)

    Hey Alchymyth,

    I ended up working out a solution using wp_get_attachment_url. I really wanted to try and get the post and use it in a image tag.

    Here’s my end result:

    <?php query_posts('p=10'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
    <img src="<?php echo $image[0]; ?>" id="image1" />
    <?php endwhile; endif; ?>

    Works pretty well

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Get featured image via post ID’ is closed to new replies.