JSON_Derulo
JSON_Derulo

Reputation: 992

How to access WordPress post thumbnail from a different post?

I'm using the same script that I wrote to use a loop to display posts of a certain category on the home page. However, now I'm using it at the bottom of a post page itself. The issue is that it is using the same thumbnail as the current post page for each post in the loop. I'm wondering how I can access the post thumbnail from external articles from within a post page.

<?php 
  // the query
    $the_query = new WP_Query( array(
    'category_name' => 'Journal',
    'posts_per_page' => 4,
)); 
?>

<div class="row row__padding--bottom">

    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="col-sm-12 col-md-3">
        <div class="journal__latest" style="background: url(<?php echo get_the_post_thumbnail_url( $post_id, 'large' ); ?>) !important; background-size: cover !important; background-position: center center !important; background-repeat: no-repeat !important;">
            <div class="post__info--container">
                <a href="<?php echo esc_url( get_permalink()); ?>"><h3><?php the_title(); ?></h3></a>
                <p><?php the_category(", "); ?></p>
            </div>
        </div>
</div>

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php __('No News'); ?></p>
<?php endif; ?>

</div>

Upvotes: 0

Views: 103

Answers (1)

Bhupender rathor
Bhupender rathor

Reputation: 34

use this code provided below forget image it will be work

<?php echo  wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) ); ?>

Upvotes: 1

Related Questions