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


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>
    $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’,
    ‘key’	=> ‘lmstartdate’,
    ‘compare’	=> ‘<=’,
    ‘value’	=> $tomorrow,
    ‘type’	=> ‘DATETIME’
    ‘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 ): ?>
    <h3><?php echo $p->post_title; ?></h3>
    <img src='<?php echo $featured_image; ?>’ alt=’Photo’ class=”thumbphoto”>
    <?php endforeach; ?>
    <?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

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

You must be logged in to reply to this topic.