Skip to content
Advertisement

How to display thumbnail image from custom post type in another post type?

I have two custom post types “book” and “author”. I want to display author’s thumbnail in book’s post card.

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $args = array(
    'post_type' => 'book',
    'posts_per_page' => 12,
    'paged' => $paged,
  );

  $report = new WP_Query($args);
?>
                            
<?php if ( $book->have_posts() ) : while ( $book->have_posts() ) : $book->the_post(); ?>
  <!-- book card -->
  <div>
    <img src="<?php echo get_the_post_thumbnail_url($post->ID, 'thumbnail'); ?>">
    <h2><?php the_title() ?></h2>

    <-- author thumbnail -->
    <img src="<?php echo get_the_post_thumbnail_url($post->ID, 'thumbnail'); ?>">
    <--// author thumbnail -->

  </div>
  <!--// book card -->
<?php endwhile; endif; ?>

I think here should be loop inside the loop, but don’t understand how to implement it.

Advertisement

Answer

If you use ACF with Post Object field type, change your <-- author thumbnail --> section with the following code

<-- author thumbnail -->
<?php if ( $book_author = get_field('book_author') ): ?>
    <img src="<?php echo get_the_post_thumbnail_url($book_author->ID, 'thumbnail'); ?>">
<?php endif; ?>
    <--// author thumbnail -->
  • don’t forget to change the field name book_author whit your field name
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement