Custom Fields
Re-Envisioned
Support General featured image from attached images

This topic contains 1 reply, has 2 voices, and was last updated by  Anh Tran 1 year ago.

  • Creator
    Topic
  • #10736

    tech500
    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

Viewing 1 replies (of 1 total)
  • Author
    Replies
  • #10751

    Anh 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 1 replies (of 1 total)

You must be logged in to reply to this topic.