user1641831
user1641831

Reputation:

Retrieving post thumbnail url from a post array with foreach loop

I'm having troubles retrieving the thumbnail of each post contained in an array.

I have this array that contains every post of a custom post type:

   <?php 
    $clients_array = array(
    'post_type' => 'clients',
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'post_status' => 'publish'
    ); 
?>

While I have no problem retrieving the thumbnail using the standard wordpress loop, like this:

<?php 
$query = new WP_Query( $clients_array );
while ( $query->have_posts() ) : $query->the_post();
?>

<?php if ( has_post_thumbnail()) : ?>

<?php the_post_thumbnail() ?>

<?php
endif;
endwhile;
?>

I'd like to load the posts with a foreach look, such as:

<?php 
$clients = get_pages($clients_array);
foreach ($clients as $page_data) {
$client_id = $page_data->ID;
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($client_id), 'thumbnail' );
echo $thumb;
}
?>

Unfortunately, I can't get it work in any way I tried.

What am I doing wrong?

Upvotes: 0

Views: 2121

Answers (2)

Ben Racicot
Ben Racicot

Reputation: 5903

Most of WordPress's functions prefixed with get_ are to retrieve the specified data and not echo it. Therefore putting the data into a variable or echoing it manually would work for your situation like @jothikannan said:

echo get_the_post_thumbnail($id);

or

$foo = get_the_post_thumbnail($client_id);
//do sowething with $foo

Upvotes: 2

Jothi Kannan
Jothi Kannan

Reputation: 3358

you must use follow to get the thumbnail of the features image

<?php echo get_the_post_thumbnail($client_id); ?>

it is already answered here look at here

Upvotes: 0

Related Questions