WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#39302 closed defect (bug) (fixed)

Twenty Seventeen: Featured image not displayed on single template

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

Description

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 3 months ago.
39302.1.patch (1.1 KB) - added by davidakennedy 3 months ago.
Adds comment with explanation.

Download all attachments as: .zip

Change History (9)

@westonruter
3 months ago

#1 @westonruter
3 months ago

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

#2 @davidakennedy
3 months 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
3 months 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
3 months 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
3 months 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
3 months ago

👍🏻 Comment looks good.

@davidakennedy
3 months ago

Adds comment with explanation.

#7 @davidakennedy
3 months 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.