Make WordPress Core

Opened 13 years ago

Closed 10 years ago

Last modified 10 years ago

#18804 closed enhancement (fixed)

Add "has-post-thumbnail" class to post_class() when there's a featured image

Reported by: danielbachhuber's profile danielbachhuber Owned by: danielbachhuber's profile danielbachhuber
Milestone: 3.9 Priority: normal
Severity: normal Version: 2.9
Component: Post Thumbnails Keywords: has-patch commit
Focuses: template Cc:


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)

postthumb.18804.1.patch (1.2 KB) - added by danielbachhuber 13 years ago.
Include 'has-featured-image' as a class if the post does and theme supports it
18804.patch (1.3 KB) - added by Frank Klein 10 years ago.
18804.2.patch (1.2 KB) - added by Frank Klein 10 years ago.

Download all attachments as: .zip

Change History (21)

13 years ago

Include 'has-featured-image' as a class if the post does and theme supports it

#1 @danielbachhuber
13 years ago

  • Keywords has-patch dev-feedback added
  • Owner set to danielbachhuber
  • Status changed from new to assigned

#2 @GaryJ
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 @donwilson
12 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) {
			$classes[] = "has-featured-image";
	return $classes;
}	add_filter('post_class', "appendPostClassHasFeaturedImageClass");

#4 @c3mdigital
11 years ago

  • Keywords needs-refresh added; dev-feedback removed
  • Severity changed from minor to normal
  • Version set to 2.9

#5 @nacin
10 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 @lancewillett
10 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.

@Frank Klein
10 years ago

#7 @Frank Klein
10 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 @seanchayes
10 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 @nacin
10 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 @Frank Klein
10 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.

#11 @nacin
10 years ago

  • Component changed from Template to Post Thumbnails
  • Focuses template added

#12 follow-up: @Compute
10 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 @SergeyBiryukov
10 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 @lancewillett
10 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

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 and 20 on

Last edited 10 years ago by lancewillett (previous) (diff)

#15 @nacin
10 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.

#16 @nacin
10 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 27429:

Add has-post-thumbnail as a post class.

props danielbachhuber, frank-klein.
fixes #18804.

#17 @lancewillett
10 years ago

In 27623:

Twenty Fourteen: remove post_class filter for has-post-thumbnail now that it's added in core for every theme. See #18804.

#18 @lancewillett
10 years ago

In 27624:

Twenty Fourteen: revert r27623 post_class filter removal since it will visually break pre-WP 3.9 installs. See #18804.

Note: See TracTickets for help on using tickets.