Understanding the WordPress template hierarchy

A little preface here. I’m going to assume you know nothing of post types or templates since I’m unfamiliar with Underscores or the Teamtreehouse development track. Hopefully the below helps clear things up if not ask further in the comments.

WordPress starts with a few built-in post types that you’re already familiar with:

  • Posts ( post type slug: page )
  • Pages ( post type slug: post )

They use their own templates, usually they go by the following:

  • Posts
    • home.php or index.php is your archive page or list of Posts
    • archive.php is where everything else goes ( archives, categories, tags )
    • single.php is where your single content is going to be.
  • Pages
    • front-page.php to assign a single pages content as your homepage.
    • page.php used for the default template of all pages
    • page-{$slug}.php which is used to target a specific page by their slug.

You could use the above with normal loops to display your content, you don’t necessarily need to use things like get_template_part() which is really just WordPress’s version of PHP include().


Now, what you’re trying to do with Portfolio’s you probably want to build your own Custom Post Type. These Custom Post Types use variations of the above templates:

  • Portfolio ( Your Custom Post Type ) – portfolio will be the slug.
    • archive-{post-type-slug}.php is where your list of portfolio posts will be.
      • archive.php will be the fallback if WordPress can’t find the above template.
      • index.php if there is no archive.php then WordPress will fallback to this template for display.
    • single-{post-type-slug}.php will be your single full portfolio post.
      • single.php will be the fallback if the above template isn’t found.

You would switch out the {post-type-slug} in the above templates with your post type slug so in the end you would have:

  • archive-portfolio.php and single-portfolio.php

Now as far as a static page for your post type you have a ton of options, none of which are right or wrong necessarily.

1) You could create an actual page for it, call it page-portfolio.php but then you would need to run a separate WP_Query() to pull your Portfolio posts because WordPress won’t know to associate that page post type with you portfolio post type. Same issue with a global Page Template but you wouldn’t need to keep track of IDs or Slugs.

2) You could allow the user to assign a page to your post type. Similar to going to the admin panel Settings -> Reading and assigning a Front Page and Blog Page. You could save a page ID into the database and on your archive-{post-type}.php get the content of that page to display it.

I’m sure there are other ways, everybody has their preferred method.


I’m not against using the above method of using template parts to separate your content but I feel it’s overly complicated for beginners when the simplified WordPress templates method is much easier to grasp.

tech