What is singular.php?

The question:

From 4.3 we have a new template, singular.php. I tried to understand what is it, but I didn’t get it. 🙂

The page.php for pages.
The single.php for every post types. If we have single-[post-type].php we can use a specific template for the custom post type.. so why we need singular.php?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It’s the nature of the WordPress template hierarchy – point being, if you don’t have a page.php, it will use singular.php, same if you don’t have single.php, it will fallback to a template lower in the hierarchy.

Ideal for themes that have the same layout for posts/pages, instead of having duplicate code in each respective template.

Method 2

singular.php is basically an extra fallback for all post types, regardless if built-in or custom. It comes in straight after single.php and page.php, so you can omit the latter two templates and just have a singular.php template which will be used by all post types in single view.

How useful it will be and the necessity of it will, just as with any other template, depend on the user/site/requirements. The template hierarchy is built such that, regardless of any page being viewed, you will always just need index.php to display any page. So in short, you can have a fully functional theme with only index.php and style.css in the theme folder.

The templates available in the template hierarchy are just there for convenience, and that goes for singular.php as well. You can use any template in context if you need to, but you don’t have to use it if you don’t need it.

Method 3

The single post template file is used to render a single post. WordPress uses the following path:

1.single-{post-type}.php – First, WordPress looks for a template for the specific post type. For example, post type is product, WordPress would look for single-product.php.

2.single.php – WordPress then falls back to single.php.

3.singular.php – Then it falls back to singular.php.

4.index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.

https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post

The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following patch:

1.custom template file – The page template assigned to the page. See get_page_templates().

2.page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.

3.page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.

4.page.php

5.singular.php

6.index.php

https://developer.wordpress.org/themes/basics/template-hierarchy/#page


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment