Support

Account

Home Forums General Issues How to get the Featured Image to show in custom post

Helping

How to get the Featured Image to show in custom post

  • I have a custom post type, and I can pull the information I need in a foreach loop, but I can’t get the featured image to show.

    How can I properly call the $featured_image? Thank you!

    <h1>Available Tomorrow</h1>
    <?php
    $today = date(‘Y-m-d’);
    $tomorrow = date(‘Y-m-d’, strtotime(“+1 day”));
    $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), ‘thumbnail’ );
    
    // query events
    $posts = get_posts(array(
    ‘posts_per_page’	=> -1,
    ‘post_type’	=> ‘last_minute_availabi’,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’	=> ‘lmstartdate’,
    ‘compare’	=> ‘<=’,
    ‘value’	=> $tomorrow,
    ‘type’	=> ‘DATETIME’
    ),
    array(
    ‘key’	=> ‘lmenddate’,
    ‘compare’	=> ‘>=’,
    ‘value’	=> $tomorrow,
    ‘type’	=> ‘DATETIME’
    )
    ),
    ‘order’	=> ‘ASC’,
    ‘orderby’	=> ‘meta_value’,
    ‘meta_key’	=> ‘lmstartdate’,
    ‘meta_type’	=> ‘DATE’
    ));
    
    if( $posts ): ?>
    <div class=”lmbox”><ul class=”events”>
    <?php foreach( $posts as $p ): ?>
    <li>
    
    <h3><?php echo $p->post_title; ?></h3>
    <img src='<?php echo $featured_image; ?>’ alt=’Photo’ class=”thumbphoto”>
    
    </li>
    <?php endforeach; ?>
    </ul></div>
    
    <?php endif; ?>
  • If you are trying to show the featured image for each of the posts in the loop, then this code needs to be done inside of the loop

    
    $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail' );
    

    Second, wp_get_attachment_image_src() returns an array https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/#div-comment-2199

    
    <img src='<?php echo $featured_image[0]; ?>' alt='Photo' class='thumbphoto'>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to get the Featured Image to show in custom post’ is closed to new replies.