#18804 closed enhancement (fixed)
Add "has-post-thumbnail" class to post_class() when there's a featured image
Reported by: | danielbachhuber | Owned by: | danielbachhuber |
---|---|---|---|
Milestone: | 3.9 | Priority: | normal |
Severity: | normal | Version: | 2.9 |
Component: | Post Thumbnails | Keywords: | has-patch commit |
Focuses: | template | Cc: |
Description
It would be nice to have an "has-post-thumbnail" or similar class added when there's a featured image associated with the post.
Attachments (3)
Change History (21)
#1
@
13 years ago
- Keywords has-patch dev-feedback added
- Owner set to danielbachhuber
- Status changed from new to assigned
#2
@
13 years ago
+1 on this idea and patch (generally).
As the existing post classes are NOT "is-sticky", "in-category" etc., then perhaps instead of "has-featured-image", it should just be "featured-image" (if that doesn't clash with any classes on or around the featured image itself)?
#3
@
13 years ago
- Cc donwilson added
+1 as well, I just had a use for it in styling post archives with posts that have featured images and those that do not, it'd be nice to have this functionality in place.
Here's a temporary filter for post_class() to provide this functionality:
function appendPostClassHasFeaturedImageClass($classes) { if(current_theme_supports('post-thumbnails')) if(has_post_thumbnail()) $classes[] = "has-featured-image"; return $classes; } add_filter('post_class', "appendPostClassHasFeaturedImageClass");
#4
@
11 years ago
- Keywords needs-refresh added; dev-feedback removed
- Severity changed from minor to normal
- Version set to 2.9
#5
@
11 years ago
- Component changed from General to Template
- Milestone changed from Awaiting Review to 3.9
I'm surprised theme developers haven't otherwise requested this. This seems like a fine idea to me. obenland, lancewillett?
#6
@
11 years ago
This would be a great enhancement, and we can remove code from default themes, like these two cases: Twenty Fourteen and Twenty Twelve.
One note: it'd be nice to avoid adding the body class when the post is password protected. In the case of Twenty Fourteen, that way the CSS hooked to the class won't change the display of the post when an image isn't going to be displayed.
#7
@
11 years ago
- Keywords needs-testing added; needs-refresh removed
Updated patch to include a check for a password protection. For the CSS class name, I used 'post-thumbnail', which is in line with the other post classes (so no 'has-' prefix) and corresponds to how the feature is commonly named on the code side (vs 'featured-image').
#8
@
11 years ago
I applied the patch and I'm seeing the additional class 'post-thumbnail' added when an attached post thumbnail, or featured image as it's called in the Dashboard, is included on the page or post.
I'm using TwentyFourteen as a test and see that it also has it's own similar function generating 'has-post-thumbnail' in the class.
If I applied a password to the content then the class was not added until the correct password was supplied.
Additionally the class 'post-thumbnail' was added to content based on a custom post type that had a post thumbnail.
#9
@
11 years ago
- Keywords commit added; needs-testing removed
I'm debating "featured-image" versus "post-thumbnail". Anyone wish to weigh in further?
The comment above current_theme_supports() is no longer accurate as these functions are always defined now. The if could probably become one line. While it is indeed possible for a post to have one of these even when the theme doesn't support it, I agree with no class there (so keep the check).
Nice call on the post passwords, too.
I'm fine with sorting out these remaining issues and committing. Thanks seanchayes for the thorough test + clear comment!
#10
@
11 years ago
I updated a patch, so that all the checks are now in a single line. This new code seems so clear that I think the additional comment explanation is no longer needed.
#12
follow-up:
↓ 13
@
11 years ago
What if the current theme includes this functionality? Then we would have 2 "post-thumbnail". While it does'nt break anything I would be alot nicer if there was a check if this class is already represented.
Using array_unique()
before the
return apply_filters('post_class', $classes, $class, $post->ID);
Might be even better.
Checking for current_theme_supports()
would be smarter to use before all this and maybe put this check into the has_post_thumbnail()
or get_post_thumbnail_id()
instead of checking for this on every line we use thumbnails.
Just 2 suggestions from my point of view :)
#13
in reply to:
↑ 12
@
11 years ago
Replying to Compute:
Using
array_unique()
before the
return apply_filters('post_class', $classes, $class, $post->ID);Might be even better.
Feel free to create a new ticket with that suggestion. get_body_class()
would benefit from it too: ticket:25906:23.
#14
@
11 years ago
I prefer has-post-thumbnail
as it's more explicit versus a class value saying this theme (or current view) supports post-thumbnails in general.
Also looking at back-compat, it's already in use in 2 default themes and 24 other themes active on WP.org.
post-thumbnail
would be my second choice, but it's already in heavy use across many themes, so could be a support headache for theme developers. I found 149 themes using it in WP.org and 20 on WP.com.
#15
@
11 years ago
We've had this schizophrenic naming of post thumbnails since 3.0 — they're post thumbnails in the code, but featured images in the UI. Markup is what I thought was a new frontier. But it turns out, the_post_thumbnail() (via wp_get_attachment_image() or whatever) includes an attachment-$size class in its markup, which works out to be "attachment-post-thumbnail". has-post-thumbnail it is.
Include 'has-featured-image' as a class if the post does and theme supports it