[Support request] Set Post Thumbnail Via Elements

Home Forums Support Set Post Thumbnail Via Elements

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1150902
    Tavis

    Hello,

    I use custom fields and Elements to generate posts. I am hoping to use a custom field to also set the post thumbnail / featured image.

    I feel like set_post_thumbnail() is the right direction, but I haven’t been able to figure it out any further.

    Any advice is appreciated!

    Thanks,
    TL

    #1151429
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You can use a filter to do this:

    add_filter( 'generate_page_hero_background_image_url', function( $url, $options ) {
        $custom_field = get_post_meta( get_the_ID(), 'your_custom_field', true );
    
        if ( $custom_field ) {
            $url = $custom_field;
        }
    
        return $url;
    }, 10, 2 );

    Let me know 🙂

    #1151495
    Tavis

    Hi Tom,

    Thanks for the quick reply!

    Where is it best to put this code? In Elements or Code Snippets?

    I am also using a custom post type if that makes a difference.

    One more thing… I am using an Amazon plugin shortcode to get the image URL. The shortcode works for pulling the URL, but I am still not getting everything to work. Any advice?

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        $asin1 = get_field("asin1");
        $asin1_img_url = do_shortcode( '[amazon fields="' . $asin1 . '" value="image"]' );
        $custom_field = get_post_meta( get_the_ID(), $asin1_img_url, true );
    
        if ( $custom_field ) {
            $url = $custom_field;
        }
    
        return $url;
    }, 10, 2 );
    #1151715
    Tom
    Lead Developer
    Lead Developer

    Code Snippets is the way to go for something like this.

    Try this instead:

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        $asin1 = get_field("asin1");
        $asin1_img_url = do_shortcode( '[amazon fields="' . $asin1 . '" value="image"]' );
    
        if ( $asin1_img_url ) {
            $url = $asin1_img_url;
        }
    
        return $url;
    }, 10, 2 );
    #1151807
    Tavis

    Hmm still not working. I also tried it with a static URL and didn’t work. Would it be possible for someone to go into my site and take a look?

    #1152219
    Tom
    Lead Developer
    Lead Developer

    Does the hero have a default background image set? If not, can you try setting a custom image as the background in the hero settings?

    #1153579
    Tavis

    No there is no default background image set. I can set a custom image background in hero settings, but I don’t see how this can allow me to dynamically set thumbnails to source external image URL via a custom field and short code.

    Any other ideas?

    #1153875
    David
    Staff
    Customer Support

    Hi there,

    I think what Tom meant was; try adding any Custom Image to the header element background.
    Then test the function he provided. Let us know.

    #1154006
    Tavis

    Ohhh, I see. Yes, that did work for the featured image. Does this method work for the post thumbnail image as well?

    #1154683
    Tom
    Lead Developer
    Lead Developer

    As long as a background image is set (featured image or custom image), the filter I shared should work.

    Let me know 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.