Meta Box
Support › General › featured image from attached images
Hello,
Is there a way to have the featured image set by the first image attached to an "image_advanced" field?
The client uploads a few images to the post, and I want the featured image to get populated by the first image that's attached.
Thanks Dan
Hi Dan,
The function get_the_post_thumbnail has a filter post_thumbnail_html that you can use to output the correct image:
get_the_post_thumbnail
post_thumbnail_html
add_filter( 'post_thumbnail_html', function( $html, $post_id, $post_thumbnail_id, $size, $attr ) { $gallery = rwmb_meta( 'field_id', array( 'size' => $size ), $post_id ); if ( empty( $gallery ) ) { return $html; } $image = reset( $gallery ); $html = wp_get_attachment_image( $image['id'], $size, false, $attr ); return $html; }, 10, 5 );