skip to Main Content

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.

2

Answers


  1. <?php if($avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
    <img src="<?php echo $avatar; ?>" alt="">
    <?php else: ?>
    <img src="/images/no-image-default.jpg">
    <?php endif; ?>
    
    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search