WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#28461 new enhancement

Add a fallback parameter to get_the_post_thumbnail() to find images

Reported by: krogsgard Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.9
Component: Post Thumbnails Keywords: needs-patch
Focuses: template Cc:
PR Number:

Description

In the 3.6 cycle, there was a lot of talk about grabbing various media types from the content for post format handling. #22960 is basically the end of that conversation, and includes a lot more stuff. #23593 is also related, but ended up getting closed in favor of #22960, which died with the post format UI.

Within that realm, the get_the_image() function from Justin Tadlock's Get The Image (https://github.com/justintadlock/get-the-image) plugin came up a bit. It's a super handy function. Although, it was mostly discussed as a piece of post formats. You can see it's got a whole bunch of functionality, but I'm mostly interested in its ability to find a fallback image in the content.

I'd like to see something like get_the_image() be able to be called via some kind of parameter within get_the_post_thumbnail() and therefore the_post_thumbnail().

Justification: There are times in building out a theme where you'd want the exact featured image or none at all (like a big wide header banner or something), but other times it's great to have any 'ole fallback, like a more traditional square thumbnail on blog archives.

In these situations, often which are theme driven, and not custom site driven, it'd be great for the theme developer to be able to assign fallback rules when they call the_post_thumbnail itself. Whether that's through a new parameter, or maybe just an additional key / value for 'fallback' to the attr parameter... I don't know. But for a theme (where most image sizes are registered anyway) to be able to define fallback behavior for those same image sizes would be powerful.

Attachments (1)

title-sandwich.png (483.2 KB) - added by obenland 5 years ago.
Title Sandwich

Download all attachments as: .zip

Change History (7)

#1 @obenland
5 years ago

If used on single posts, that could lead to unwanted post title sandwiches. I wonder if it wouldn't just lead to more user frustration and confusion, when featured images show up without explicitly setting them. I'm sure there would also be a lot of instances where no image in the post content would be a good fit as a representation for the entire post.

@obenland
5 years ago

Title Sandwich

#2 @krogsgard
5 years ago

@obenland I agree, that's why I think it should be an optional parameter, to be set by the theme developer.

From my original notes:

Justification: There are times in building out a theme where you'd want the exact featured image or none at all (like a big wide header banner or something), but other times it's great to have any 'ole fallback, like a more traditional square thumbnail on blog archives.

There could still be an issue of sandwiching if someone switching themes that handle things differently, but that's part of any theme switch -- some maintenance. Also, for instances where the featured image is called with an excerpt or something, I'd imagine most folks would want a default featured image from content vs nothing.

I've found that many people just don't fully grasp the featured image. Additionally, for really old content, setting featured images on a bunch of old posts is a pain.

I won't claim to have the perfect solution, but I think putting some functionality in here somewhere would end up being a very well accepted feature, if done responsibly.

This ticket was mentioned in Slack in #core-flow by krogsgard. View the logs.


5 years ago

#4 @wonderboymusic
4 years ago

  • Keywords needs-patch added

#5 @flixos90
4 years ago

This appears to be rather plugin/theme territory to me.

What would be useful however to make this possible in a plugin is that we could add a new filter get_post_thumbnail_id inside the function of the same name. This would allow to override the post thumbnail - not actually setting it though, but it would allow to add a fallback in the frontend. We could also limit this filter to only be executed if there is no actual featured image for that post.

To have this filter only be run in the frontend, we could add an additional parameter $filtered with default true and explicitly set it to false wherever calling it from the admin. Alternatively we could not use the function at all in the admin - it is only used in one location there anyway (most areas access the post meta directly), so we're a bit inconsistent atm.

@krogsgard: Thoughts on this?

Last edited 4 years ago by flixos90 (previous) (diff)

#6 @swissspidy
3 years ago

get_post_thumbnail_id can already be filtered via the post meta filter, targeting _thumbnail_id.

Note: See TracTickets for help on using tickets.