使用foreach循环从post数组中检索post缩略图url


Retrieving post thumbnail url from a post array with foreach loop

我在检索数组中包含的每个帖子的缩略图时遇到问题。

我有这个数组,它包含自定义帖子类型的每个帖子:

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

虽然我使用标准的wordpress循环检索缩略图没有问题,比如:

<?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;
?>

我想加载带有前臂外观的帖子,例如:

<?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;
}
?>

不幸的是,我无论如何都做不到。

我做错了什么?

WordPress的大多数以get_为前缀的函数都是检索指定的数据,而不是回显它。因此,将数据放入变量或手动回显它将适用于您的情况,如@jothikannan所说:

echo get_the_post_thumbnail($id);

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

您必须使用follow来获取特征图像的缩略图

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

这里已经回答了,看看这里