Are you passing in a post ID to has_post_thumbnail()
? Without seeing the code and in what context it’s being used, it’s hard to say.
Also, some related tickets:
– https://github.com/WordPress/gutenberg/issues/32447
– https://github.com/WordPress/gutenberg/issues/34865
Hi. Thanks for getting back to me. I **may** have found a solution. It works, but is it appropriate/correct/best practice?
What I have is a header-default.php pattern that contains the following conditional check…
$post_id = url_to_postid($_SERVER[‘REQUEST_URI’]);
if (has_post_thumbnail($post_id)) {
$content = ‘pattern will include a cover block that uses the featured image’;
} else {
$content = ‘pattern will display something different’;
}
The main contention is with regard to how the post_id is obtained, which appears a bit ‘hacky’, but it is the only way I could get the post_id to work within the context of it being used in a pattern.
First impressions suggest that this will work as desired.
Any thoughts/suggestions appreciated.
Thank you
If this is for a single post view (I’m assuming it is since you’re using the URL), you can grab the post ID via get_queried_object_id()
: https://developer.wordpress.org/reference/functions/get_queried_object_id/
Hi. This would be for any post/page/custom content type where a featured image can be set. I have tried to use the get_queried_object_id() within the template but it only ever returns 0. Thanks
For patterns, this probably isn’t a great idea. Because they are processed when registered, the global query hasn’t run yet.
Also, any solution that relies on conditionals or server-side PHP data will only work by default on the front end (including yours). If/When a user saves the pattern in the editor, it will be whatever the fallback is.
You can probably hook into render_block
on the server-side to add a fallback at that point: https://developer.wordpress.org/reference/hooks/render_block/
Hi. OK thanks, will look in to render_block, thanks for your help.