0

I'm not familiar with working outside the loop so forgive me if this question has been asked already.

How do I get a featured image of a post that's outside the loop? I did a print_r and I didn't see anything related to a featured image.

Any ideas on what to do next?

Update: I was able to get it working but I need a way to show every image size associated with the post's featured image.

This is what I've done so far:

// the query. Only show posts with a certain taxonomy
<?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published')))); $query = new WP_Query( $args );?>
<?php if ( $query->have_posts() ) : $major = false; $major_first = false; $duplicates = []; while ( $query->have_posts() ) : $query->the_post(); ?>

//check if any post belongs to a certain category
<?php if ( in_category( 'major' ) ) : ?>
    <?php $major = true;?>
    <?php $major_data [] = get_post($post->ID) ;?>
<?php endif; ?>
<?php if ( in_category( 'major-first' ) ) : ?>
    <?php $major_first = true;?>
    <?php $major_first_data [] = get_post($post->ID) ;?>
    <?php $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'large' ); $image = $image_array; ?>

<?php endif; ?>

// end of the loop
<?php endwhile; endif; ?>

// show content
<?php if ($major == true):?>
    <?php if ($major == true):?>
        <?php foreach($major_first_data as $postID) { ;?>
            <?php $postData = get_post( $postID );?>
            <?php print $postData->post_title;?><br>
            <?php echo $image[0]; ?>
        <?php } ?>
    <?php endif;?>
    <?php foreach($major_data as $postID) { ;?>
        <?php $postData = get_post( $postID );?>
        <?php print $postData->post_title;?><br>
    <?php } ?>
<?php endif;?>
0

First you will need to get a list of all available sizes on your site. This function get_intermediate_image_sizes() will do that. You can then pass the results of that to image_get_intermediate_size() along with the post id and

<?php $sizes = get_intermediate_image_sizes(); ?>
<?php foreach($major_data as $postID) { ;?>
    <?php $postData = get_post( $postID );?>
    <?php print $postData->post_title;?><br>
    <?php var_dump(image_get_intermediate_size( $postID, $sizes )); ?> 
<?php } ?>

more info on this function get_intermediate_image_sizes()

lot of good functions for dealing with images in the media.php wordpress core file

0

I was able to get it working by doing the following:

<?php foreach($major_first_data as $postID) { ;?>
<?php $postData = get_post( $postID );?>
<?php print $postData->post_title;?><br>
<?php print wp_get_attachment_image_src( get_post_thumbnail_id( $postID ), 'thumbnail' )[0];?>
<?php } ?>

Focusing on the line:

<?php print wp_get_attachment_image_src( get_post_thumbnail_id( $postID ), 'thumbnail' )[0];?>

it outputs the following:

Array ( [0] => url to thumbnail [1] => image width [2] => image height [3] => 1 )

And it you want to use another size, just substitue thumbnail to anything else you want.

Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.