Opened 8 years ago
Closed 2 years ago
#38160 closed enhancement (wontfix)
Attachment template hierarchy selects generic attachment.php before specific post type templates
Reported by: | johnbillion | 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.php
jpeg.php
image.php
attachment.php
single-attachment-foo.php
single-attachment.php
single.php
singular.php
index.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/38160
with files likeattachment.php
andsingle-attachment-foo.php
appart fromindex.php
andstyle.css
.Then switch to that theme with:
where we could add our test method to the
Tests_Admin_includesTheme
class to be able to use thesetUp
there src, or move the similar setup to another class.Then create an attachment and go to that attachment page.
Then try using
get_attachment_template()
andget_single_template()
to verify the wanted results.But here
locate_template()
only returned an empty string, because theTEMPLATEPATH
was stuck for the default themetests/phpunit/includes/../data/themedir1/default
.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 theTEMPLATEPATH
is also stuck there for thedefault
theme.Maybe I'm missing something here, but I will have to look into the tests further.