- This topic has 9 replies, 3 voices, and was last updated 4 years, 2 months ago by Tom.
-
AuthorPosts
-
January 31, 2020 at 4:15 pm #1150902Tavis
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,
TLFebruary 1, 2020 at 9:19 am #1151429TomLead DeveloperLead DeveloperHi 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 ๐
February 1, 2020 at 10:17 am #1151495TavisHi 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 );
February 1, 2020 at 4:33 pm #1151715TomLead DeveloperLead DeveloperCode 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 );
February 1, 2020 at 10:27 pm #1151807TavisHmm 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?
February 2, 2020 at 9:35 am #1152219TomLead DeveloperLead DeveloperDoes the hero have a default background image set? If not, can you try setting a custom image as the background in the hero settings?
February 3, 2020 at 8:13 pm #1153579TavisNo 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?
February 4, 2020 at 4:14 am #1153875DavidStaffCustomer SupportHi 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.February 4, 2020 at 6:22 am #1154006TavisOhhh, I see. Yes, that did work for the featured image. Does this method work for the post thumbnail image as well?
February 4, 2020 at 3:27 pm #1154683TomLead DeveloperLead DeveloperAs long as a background image is set (featured image or custom image), the filter I shared should work.
Let me know ๐
-
AuthorPosts
- You must be logged in to reply to this topic.