Opened 9 years ago
Closed 4 years ago
#38160 closed enhancement (wontfix)
Attachment template hierarchy selects generic attachment.php before specific post type templates
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | |
| Component: | Themes | Keywords: | needs-unit-tests |
| Focuses: | template | Cc: |
Description
When viewing a single attachment at its permalink, the template hierarchy is as follows (using foo.jpg as an example).
image-jpeg.phpjpeg.phpimage.phpattachment.phpsingle-attachment-foo.phpsingle-attachment.phpsingle.phpsingular.phpindex.php
Note that the generic attachment.php appears higher in the hierarchy than the more specific single-attachment-foo.php. This means it's impossible to target a specific attachment if an attachment.php file is in place.
Attachments (1)
Change History (3)
Note: See
TracTickets for help on using
tickets.
In 38160.diff the
get_attachment_template()andget_single_template()are modified accordingly. There are various ways to handle this, but I think this approach has the smallest code change that I could think of.I looked into creating a test, but stumbled upon a problem.
The plan is to create a theme under
tests/phpunit/data/themedir1/38160with files likeattachment.phpandsingle-attachment-foo.phpappart fromindex.phpandstyle.css.Then switch to that theme with:
where we could add our test method to the
Tests_Admin_includesThemeclass to be able to use thesetUpthere src, or move the similar setup to another class.Then create an attachment and go to that attachment page.
$attachment_id = self::factory()->attachment->create_object( 'foo.jpg', 0, array( 'post_name' => 'foo', 'post_mime_type' => 'image/jpeg', ) ); $this->go_to( get_permalink( $attachment_id ) );Then try using
get_attachment_template()andget_single_template()to verify the wanted results.But here
locate_template()only returned an empty string, because theTEMPLATEPATHwas stuck for the default themetests/phpunit/includes/../data/themedir1/default.I also tried to see if a child theme of default would make any difference, but it didn't.
I checked the tests in
Tests_Theme::test_switch_theme():https://core.trac.wordpress.org/browser/tags/4.8.2/tests/phpunit/tests/theme.php#L285
that uses
get_attachment_template()andget_single_template(), but they are also only returning an empty string, because theTEMPLATEPATHis also stuck there for thedefaulttheme.Maybe I'm missing something here, but I will have to look into the tests further.