Make WordPress Core

Opened 11 years ago

Closed 9 years ago

Last modified 9 years ago

#22314 closed enhancement (fixed)

Add singular.php to template hierarchy

Reported by: chipbennett's profile chipbennett Owned by: jorbin's profile jorbin
Milestone: 4.3 Priority: normal
Severity: normal Version: 3.4.2
Component: Themes Keywords: has-patch needs-codex needs-testing needs-refresh 2nd-opinion
Focuses: template Cc:

Description

Currently, the template hierarchy includes a fallback template (archive.php) for all archive-index type pages (date, tag, category, taxonomy, author, blog posts index, custom post type index), but does not include an analogous fallback template for single-post type pages (blog post, page, attachment, custom post type). This patch adds singular.php to act as such fallback.

This change also ensures that all template context conditionals have associated template files. AFAIK, is_singular() is currently the only template context conditional that does not have such a corresponding template file.

Attachments (2)

singular.php.diff (1.2 KB) - added by chipbennett 11 years ago.
Adds support for singular.php template file
singular.php.1.diff (1.2 KB) - added by chipbennett 11 years ago.
Fixes get_singular_template() typo

Download all attachments as: .zip

Change History (26)

@chipbennett
11 years ago

Adds support for singular.php template file

#1 @SergeyBiryukov
11 years ago

  • Component changed from General to Template

#2 follow-up: @cais
11 years ago

A few questions ...

Is this meant to be a generic template for all single views essentially providing a merged single and page template, rather than falling directly back to index?

Although it would add a consistency to the template hierarchy, and possibly reduce a theme's payload by one (duplicated) file, what are the benefits to adding this template?

Would there be a similar benefit to having page fallback to single (or vice versa) instead?

#3 @sabreuse
11 years ago

  • Cc sabreuse added

#4 in reply to: ↑ 2 ; follow-up: @chipbennett
11 years ago

Replying to cais:

A few questions ...

Is this meant to be a generic template for all single views essentially providing a merged single and page template, rather than falling directly back to index?

Although it would add a consistency to the template hierarchy, and possibly reduce a theme's payload by one (duplicated) file, what are the benefits to adding this template?

I think the consistency is the benefit, in large part. Also, to me, it makes sense to have (the option to have) one template for outputting an index of posts, and one template for outputting a single post.

Would there be a similar benefit to having page fallback to single (or vice versa) instead?

Not really, IMHO; at least, no moreso than it would make sense to have category.php fallback to tag.php.

#5 in reply to: ↑ 4 ; follow-up: @cais
11 years ago

Replying to chipbennett:

I think the consistency is the benefit, in large part. Also, to me, it makes sense to have (the option to have) one template for outputting an index of posts, and one template for outputting a single post.

Does not the single template serve to display a single post? I am not inherently against the idea of adding another template; this discussion is meant more as a consideration of the current template choice logic and if it should be adjusted to take into account what you are suggesting your proposed singular template accomplish.

#6 in reply to: ↑ 5 @chipbennett
11 years ago

Replying to cais:

Replying to chipbennett:

I think the consistency is the benefit, in large part. Also, to me, it makes sense to have (the option to have) one template for outputting an index of posts, and one template for outputting a single post.

Does not the single template serve to display a single post?

Not for all post types, whereas the archive-index pages for all posts types will fallback to archive.php. The exception for single-post pages is the page post-type, which falls back directly to index.php. All the rest - post, attachment, and custom post types - fall back to single.php, then to index.php.

The analogy would be something like having author.php fallback directly to index.php, while all other index-archive type pages falling back to archive.php.

I am not inherently against the idea of adding another template; this discussion is meant more as a consideration of the current template choice logic and if it should be adjusted to take into account what you are suggesting your proposed singular template accomplish.

No worries. I'm just throwing the idea out there. If it gets implemented, then great. If not, then at least the reasoning will be documented here, should anyone ask about the (minor) inconsistency in the future.

#7 follow-up: @toscho
11 years ago

  • Cc info@… added

There is a typo in get_singlular_template(). Should be get_singular_template().

@chipbennett
11 years ago

Fixes get_singular_template() typo

#8 in reply to: ↑ 7 @chipbennett
11 years ago

Replying to toscho:

There is a typo in get_singlular_template(). Should be get_singular_template().

Aaargh. Fixed.

#9 @mbijon
11 years ago

  • Cc mike@… added

#10 @nacin
10 years ago

  • Component changed from Template to Themes
  • Focuses template added

#11 @cramdesign
10 years ago

This addition seems to make a lot of sense and would parallel is_single(), is_page() and is_singular().

#12 @knutsp
10 years ago

+1

is_singular() is currently the only template context conditional that does not have such a corresponding template file.

This seems a very natural addition to the template hierarchy. This means one can make a common template for all singular content and keep index.php more dedicated to showing multiple content. singular.php has become a "missing link" and it has to do with the introduction of custom post types, a special front page and an alternative "blog" page (posts index), and finally with the introduction of is_singular().

When making a child theme for a (multi) site with (usually, a common theme and) many custom post types I want to keep the templates collection as simple as possible. First I create special templates for CPT archives, tax archives and single CPTs with custom metadata. Other, more plain content, like posts, pages, other CPTs in singular form should be able to share a template file, sometimes with simple conditionals for the display, or not, of standard metadata (author, date etc).

#13 @jorbin
9 years ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to 4.3

@since Docs need an update. Along with that, it would be nice for the patch to be created from the root and not the wp-includes directory.

I like this idea. Has the potential to make some themes simpler.

#14 follow-ups: @johnbillion
9 years ago

  • Keywords 2nd-opinion added

I'm not too enthused by this. For all post types apart from Pages, singular.php is functionally identical to single.php (ie. it provides no more granular control, unlike the rest of the hierarchy). It's too narrow a use case to warrant introducing a new file into the template hierarchy.

If you want Pages to use the same template as other post types, you can do this by putting include 'single.php'; into page.php.

The added template hierarchy complexity of introducing singular.php outweighs its small benefit.

#15 in reply to: ↑ 14 @obenland
9 years ago

Replying to johnbillion:

The added template hierarchy complexity of introducing singular.php outweighs its small benefit.

I'm not sure I agree with it making the template hierarchy more complex. You could also argue it makes it simpler, since the template tag has a corresponding template now, completing the hierarchy and not confronting theme authors with the special case of it missing.

I'm not too enthused by this. For all post types apart from Pages, singular.php is functionally identical to single.php (ie. it provides no more granular control, unlike the rest of the hierarchy). It's too narrow a use case to warrant introducing a new file into the template hierarchy.

I agree, in most cases you'd probably still end up with at least two templates, singular.php and a more specialized template for either pages or single post, just because they are so different in their requirements. But it would certainly help themes to provide a default template for hierarchical custom post types.

If you want Pages to use the same template as other post types, you can do this by putting include 'single.php'; into page.php.

That's the reason we have so many templating possibilities in the first place, to avoid doing that. Working on Twenty Thirteen there was a big discussion about whether it should ship with an archive.php with a lot of conditionals or more specific templates for tags, categories, etc. The reasoning was the same, let's use the appropriate template, that's why we have that hierarchy in the first place.

#16 @grantpalin
9 years ago

I also like the idea of having one singular fallback template to match the common archive fallback template. It will be a minor change to theme building, but it will promote consistency, always a good thing.

This ticket was mentioned in Slack in #themereview by chipbennett. View the logs.


9 years ago

#18 @obenland
9 years ago

  • Owner set to jorbin
  • Status changed from new to assigned

This ticket was mentioned in Slack in #themereview by chipbennett. View the logs.


9 years ago

#20 in reply to: ↑ 14 @jorbin
9 years ago

Replying to johnbillion:

I'm not too enthused by this. For all post types apart from Pages, singular.php is functionally identical to single.php (ie. it provides no more granular control, unlike the rest of the hierarchy). It's too narrow a use case to warrant introducing a new file into the template hierarchy.

I don't think it's too narrow. I think it's the natural progression that should have been done a long time ago.

If you want Pages to use the same template as other post types, you can do this by putting include 'single.php'; into page.php.

The added template hierarchy complexity of introducing singular.php outweighs its small benefit.

I think it actually reduces complexity since developers will no longer need to remember that this is the one template tag that doesn't match the conditionals.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


9 years ago

#22 @peterwilsoncc
9 years ago

+1

<?php
get_template_part( 'single' );

This has been my page.php on client and personal projects for years, per @jorbin's comment above it can become a little counter-intuitive around parent/child themes & generally keeping track.

#23 @obenland
9 years ago

I think we should do it. And soon, beta is looming.

#24 @jorbin
9 years ago

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

In 32846:

Add singular.php to template hierarchy

Singular is the only template context conditional that lacks a corresponding template. This allows some themes to simplify.

Fixes #22314.
Props chipbennett.

Note: See TracTickets for help on using tickets.