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 The WordPress Template Hierarchy Page and Post Templates in WordPress Static Page Template Files

When to use content-pagename .php ?

I have seen themes using content-pagename.php in many themes and also in the videos here. This file was used by get_template_part function and now in this video, pages are named as page-slug.php or page-id.php. However, I am not clear about the "WHEN TO USE WHICH ". For example, I want to have an about us page template. Should I use page-about-us.php naming for it or content-about ?

Please elaborate !!

Disclaimer- I am a wordpress learner myself, so take my advice with pinch of salt :)

page-$slug.php or page-$id.php - they are page level templates. Means they will contain everything starting from header, body, content, sidebars and finally footer.

abc-xyz.php are template parts. They are not used "on their own" but they can be reused in other "page level templates" to define certain parts of the page.

eg if you look at twentyfourteen theme, you can see content-pagename.php is a template part which is being used in page.php as well as full-width.php by using this line-

                    // Include the page content template.
                    get_template_part( 'content', 'page' );

So it's not either-or decision, you definitely need a page level template like page.php, additionally to make your life simple you may or may not need a template part.

Let me know if it makes sense.

Thanks Avneendra. It makes sense to me. I hope that the usage you have described is the only way as these are to be used in Wordpress. Will wait for someone to confirm this or have some alternative usage. Thanks again man !

3 Answers

Satnam Singh
Satnam Singh
7,194 Points

Hey Kushal,

You use page-$slug.php for custom page templates, compare content-$slug.php with header.php or footer.php, so if we want to import any repetitive codes on any page or anywhere, we keep those codes at one place inside content-$slug.php and import that content by using this simple function :

<?php get_template_part('content', '$slug'); ?>

Same thing we do with get_header() to import header.php codes into index.php files.

Hope it's clear now :)

Best Regards, Satnam Singh

Tammy Hart
Tammy Hart
2,819 Points

Kushal,

Avneendra is correct. page-{id/slug}.php is used automatically by the page hierarchy and should include header, footer, etc. Template parts are great for making your code clean and reusable by sectioning off bits of code.

Ok. Great !