featured image from attached images

Support General featured image from attached images

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #10736
    tech500tech500
    Participant

    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

    #10751
    Anh TranAnh Tran
    Keymaster

    Hi Dan,

    The function get_the_post_thumbnail has a filter post_thumbnail_html that you can use to output the correct image:

    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 );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.