Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#39302 closed defect (bug) (fixed)

Twenty Seventeen: Featured image not displayed on single template

Reported by: westonruter's profile westonruter Owned by: davidakennedy's profile davidakennedy
Milestone: 4.7.1 Priority: normal
Severity: normal Version:
Component: Bundled Theme Keywords: has-patch
Focuses: Cc:


I assigned a featured image to a post and it appeared as expected in the loop. When I navigated to a given post that had the featured image, however, it was nowhere to be seen. In looking further, the call to display the featured image is in header.php before the_post() is called and thus the $post global is not set. This means that has_post_thumbnail() return false and no featured image will be set. To reliably get the featured image, we need to explicitly pass in get_queried_object_id().

Attachments (2)

39302.0.diff (869 bytes) - added by westonruter 7 years ago.
39302.1.patch (1.1 KB) - added by davidakennedy 7 years ago.
Adds comment with explanation.

Download all attachments as: .zip

Change History (9)

7 years ago

#1 @westonruter
7 years ago

  • Keywords has-patch added
  • Owner set to davidakennedy
  • Status changed from new to assigned

#2 @davidakennedy
7 years ago

Hey @westonruter! Thanks for the report!

Can you provide some screenshots or what you're seeing? And describe where you're expecting the featured image to show up?

I tested this, both with posts and pages. With existing ones and new ones and wasn't able to reproduce the lack of a featured image when viewing the single post and/or page.

#3 @westonruter
7 years ago

Humm. I can't reproduce this anymore either. It might have been a plugin that failed to do wp_reset_postdata() before get_header() is called. Nevertheless I think the theme would be more robust if it explicitly referenced the main queried post rather than assume that $post global is set.

#4 @celloexpressions
7 years ago

+1 for hardening the logic per 39302.0.diff, but let's also include an inline comment explaining why $post may not be what you'd expect it to be here.

#5 @davidakennedy
7 years ago

I'm up for still committing the patch because, like you all said, it's a good thing to harden. As for the comment, I could add that. Maybe this?

Using get_queried_object_id() here since the $post global may not be set before a call to the_post().

#6 @westonruter
7 years ago

👍🏻 Comment looks good.

7 years ago

Adds comment with explanation.

#7 @davidakennedy
7 years ago

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

In 39624:

Twenty Seventeen: Hardens the logic for calling featured image in header.php

This change uses get_queried_object_id() here since the $post global may not be set before a call to the_post().

Props westonruter.

Fixes #39302.

Note: See TracTickets for help on using tickets.