• Hi. I’ve attempted to use a cover block, with the featured image option selected, within a full site editing header template. This works well when the respective post/page does have a featured image set, but in the event that there’s not one set, I want to serve up content differently, be it a default image or differently styled content or whatever. However, attempts to determine if the post/page has a set featured image do not work, for example, using pre FSE approaches such as testing has_post_thumbnail(). Has anyone come across this and/or have a solution? Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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

    Thread Starter jonmackintosh

    (@jonmackintosh)

    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/

    Thread Starter jonmackintosh

    (@jonmackintosh)

    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/

    Thread Starter jonmackintosh

    (@jonmackintosh)

    Hi. OK thanks, will look in to render_block, thanks for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Detecting when there’s no Featured Image in an FSE context’ is closed to new replies.