Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

What's a proper file template naming for singular static pages

I've done quite a bit of reading in the Codex as well as i've finished most parts of the "Build a WordPress Theme" here on Treehouse but there is still one thing i still quite get - theme template file naming, in particular with singular static ones.

Navigation and function wise the test site i play around with works already fine but i am still curious what the right naming convention for template files would be. The following scenario:

Front Page - Landing page with two brief loops for example projects and the last three blog posts and a bit of hardcoded content (front-page.php) Posts Site - Blog page (home.php)

Both pages were set within wp-admin/settings/reading … the following pages were defined in wp-admin/pages and consists of pages with hardcoded content and loops sometimes.

404 - 404 page (404.php) categories - categories page (category.php) about - about page with hardcoded content and a loop for collecting team members (page-about.php) contact - contact page with hardcoded content (page-contact.php) projects- project overview dynamical with loops (page-projects.php) single project - detailed view of a single project (single-projects.php) blogpost - individual blog post (single.php) general blog related content - the logic for querying the blog posts is packed in one php file like described in the course (content-blog.php)

i understand the naming of the standard files like category.php, single.php, 404.php and so on. The only uncertainty i have is with singular static pages like the ones i use for e.g. about and contact which contain hardcoded static content as well as sometimes a loop. Per definition those are singular pages as well as static so you have the following opportunities namingwise:

custom (e.g.contact.php) or default (e.g. page-contact or page_contact)

so what would be the recommended best practice. in the codex and blog posts it isn't recommended to use complete custom file names as well as the page- prefix. in a side note i only read once that page_ might be a viable option. but how to handle that naming in general? what would be the cleanest and most recommendable way? Just try to get and understand things in that special field. Any hints and explanations appreciated. ;)

Best regards Ralf

4 Answers

http://codex.wordpress.org/Template_Hierarchy

Just to be clear, your front-page.php did NOT show up as the homepage because you set it in options, it showed up as the homepage because it's named front-page.php. WP parses the template hierarchy before it hits the DB. All this is covered in that codex article. You should carefully review the template hierarchy. Also your blog probably didn't show up due to your settings either, but because it was called home.php which has special meaning.

Page templates are not defined by their names (usually), although most people name them page_x just to keep them together. They are set by a line in the comments at the head of the template. You can then assign pages this template in the admin.

Now the reason people use page_ instead of page- as a prefix is that page- is part of the template hierarchy. In fact, it's the first check for any page content. The template hierarchy will search for page-{slug} and page-{ID} after checking for a manually set template. So naming them page_ instead will make sure they won't accidentally be triggered by a slug or ID collision.

uhhhhh excellent remarks about the reason why the pages (front-page & home) showed up. thanks!

as well as the explanation about page- and page_ … cool! and does that mean if i would go with template- , like mentioned in one of the linked articles above, instead of page_ it would work too? because template- isn't in the hierarchy like page_ - so no interferences?

thanks again!

Yes, this is correct, you can use a custom prefix like template-, you just have to make sure to add the necessary comment at the head of the template PHP.

Nick Stellato
seal-mask
.a{fill-rule:evenodd;}techdegree
Nick Stellato
Front End Web Development Techdegree Student 33,994 Points

Ralf,

Instead of re-wording an entire post, here is a link to an explanation on tutsplus. This gives a concise description of a good file naming construct.

-Nick

Thanks Nick. Basically that means instead of using a custom file name or the page- / page_ prefix to prefer template- as type prefix? Thanks!

Matt Campbell
Matt Campbell
9,767 Points

Hi,

The about page and contact page are NOT singular pages. They are simply templates that you've called about and contact, to describe what they are. You could call them spacehopper and pogostick but, you'd have no idea what file was for what. Also, there's zero need to hardcode anything in WP. Simply put a very short loop of the_title and the_content to get the page title and anything you write in the editor, such as shortcodes for putting plugins on that page. WP defaults to the page's content rather then get posts without a post loop. As a backup, I advise putting something in like the_post() before so that it gets that page's content. Couple of fallbacks occasionally needed.

Anyway, the single-pagename relates to template hierarchy rather then naming convention. You use it when you have an archive page of all the posts that relate to a category or post type for example. Then when you click on one of the posts to read more, it will default to page.php or single.php so if you want a custom page for differing post types, for example, then you'd write single-pagename as archive-pagename single post page. Then you put at the top of the single template, if(is_singular('pagename') : the_post()

Remember, the word after the hyphen on both archive and single must be identical. WP checks for single templates before going to the default. If name is different, it won't find it.

Look at template hierarchy to understand more. Hope that helps.

uhhhh my bad i basically thought singular pages, like they are called in the template hierarchy, refer to plain template pages. but i am slowly grasping the idea behind and my error in reasoning. ;)

by the way, i've got one further question about one of your recommendations. you've mentioned there would be zero need to hardcode any content into a template. This is the only part about wordpress i am still not quite comfortable about. ok i am down with it if your content consist of one title and one long text block - for one page it's understandable for me. But if you have on one page e.g. only three headline and subline snippets and three or four short about-snippets for overlays then i have my issues getting those unconnected pieces of text under one roof into the wysiwg editor for that specific page. but thats another story. ;) still need to get to know wordpress a bit more i suppose. ;)

thanks!

Matt Campbell
Matt Campbell
9,767 Points

If you want more individual pieces of content to manipulate how you want, then that is where the power of meta boxes comes into it's own and perhaps custom post types.

You could create a meta box that with some jQuery, you can add as many text areas/text fields to give you access to creating more pieces of content that are standalone.

I can't walk you through meta boxes and advanced meta boxes but keep at it, read up on it and once learnt, you'll love them!

Way way way better then custom fields or advanced custom fields. Only reason to use a plugin is so you can use the data in another theme but, there's going to be no code to call that data so it's kind of null and void in that respect. You can just move the function to a new functions.php file if you want it in a new theme and it'll be there as it was before because the data is always in the database.

so far i've used only custom post types and advanced custom fields on the plugins side. but i will give the meta boxes thing a read. thanks!

Yeah otherwise the template wouldn't show up in the list of available templates to assign to a page in the admin-interface. cool cool cool. i guess i finally got the whole naming and hierarchy template thingy. makes sense now. thanks a lot!