I know this has been investigated before but no one has given a solution. Besides, I have not enough Stack-reputation to add comments, so I have to ask a new question.
I want to use a post's custom-field ("fieldname") value as featured image.
I would like to create one single function-to-fit-all.
I come from this, but with no luck:
add_filter('get_the_post_thumbnail', function($html) {$post = get_post();
if (! $post) { return $html; }
if (! in_array($post->post_type, array('fieldname-one', 'fieldname-two'), true)) { return $html; }
$newThumbnail = filter_var( get_post_meta($post->ID, 'fieldname-one', 'fieldname-two', true), FILTER_SANITIZE_URL );
if($newThumbnail) { $html = '<img src="' . esc_url($newThumbnail) . '" alt="" />'; }
return $html;
});
Can anyone point me to the right direction? Thank you