只获取特色图片

get only featured image

我在网上搜索,但我只找到了反向场景,我想做的是在这个查询中只获取特色图片

$attachments = new WP_Query( array(
                'post_parent__in' => $published,
                'post_type' => 'attachment',
                'post_status' => 'inherit',
                'fields' => 'ids',
                'posts_per_page' => 1,
                'orderby' => 'rand',
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false,
                'no_found_rows' => true
            ) );

            if ( $attachments->have_posts() ) {
                $image = wp_get_attachment_image_src( $attachments->posts[0], $args[ 'size' ] );

                if ( file_exists( $image[0] ) ) {
                    set_transient( $objects_key, $image, 3 * HOUR_IN_SECONDS );
                }
            }
        } 

你真的不想搞乱 wp_query() 如果你能帮助它试试这个...

global $post;
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if($post_thumbnail_id != null) {
  $image = wp_get_attachment_image_src( $post_thumbnail_id, $args[ 'size' ] );
  if ( file_exists( $image[0] ) ) {
    set_transient( $objects_key, $image, 3 * HOUR_IN_SECONDS );
  }
}

我假设 $objects_key 和 HOUR_IN_SECONDS 是在别处定义的