0

I can't find a comprehensive list of criteria for how WordPress determines which archive type template it chooses.

From what I understand it uses URL forms for this purpose, like:

example.com/category/some-category -> category.php (i guess tags work similarly)
example.com/2012 -> date.php

But what about author.php and taxonomy.php? What triggers the template hierarchy to use those routes?

My ignorance is about how WP selects certain templates for I which I don't know how to trigger. I think I understand category templates with higher specificity, and archive.php I understand as a catch-all before going to index.php, but I have no idea when WP will select for example author.php and why.

For example, suppose I have two posts by the same author. How do I make WP use the author.php template to show me the posts by that author?

I suppose the notes around each box in the template hierarchy chart is supposed to give some clue about this, but I don't understand when and where those pieces of code should be used. I did look at the hierarchy chart, but there are things about it I don't understand, which is part of the reason I'm posing this question.

For example, I can not read from the chart that if I have a URL on the form example.com/2012 it will use the date.php template. It makes sense, but I can't see that the URL should be on any form, especially this one.

In general I have a hard time understanding how WordPress selects which template(route) to take. I may have misunderstood, but I expect WordPress to determine which template to use before it starts interpreting its contents. At that point it can display anything you want depending on its contents and using the is_sometype() functions. What I don't understand is how it reaches that point. Have I got this reasoning wrong? I'd be very grateful if someone could address this point.

edit: Some follow-up questions after Chip's answer:

Do I understand it correctly that WP parses permalinks using rewrite rules to build the query, or else the query is already in "native" format, i.e. a URL followed by ? and a concatenation of query variables? Can a combination of both occur?

What is the definition of "current context" with regard to queries? Could you give an example?

What do the blue .keywords signify that are below each box in Chip's diagram?

3

2 Answers 2

5

WordPress uses a Template Hierarchy to determine which template file to load based on the current context:

WordPress Template Hierarchy

This diagram is a visual representation of \wp-includes\template-loader.php, that contains the context-based template-selection logic.

As to your specific questions:

For example, suppose I have two posts by the same author. How do I make WP use the author.php template to show me the posts by that author?

WordPress does this automatically. Assuming you have pretty permalinks enabled, the author archive for a given author is: example.com/author/{author}

For example, I can not read from the chart that if I have a URL on the form example.com/2012 it will use the date.php template. It makes sense, but I can't see that the URL should be on any form, especially this one.

I think you're approaching it exactly backwards. Again, if you have pretty permalinks enabled, and are viewing the date-based archives for a given year, month, or day, the URL will be:

  • example.com/{year}
  • example.com/{year}/{month}
  • example.com/{year}/{month}/{day}

But most importantly: none of that has anything directly to do with the template hierarchy. Regardless of the permalink structure, WordPress asks, "is this a date archive?" If yes, then WordPress uses the date-based archive template hierarchy to determine which template file to load.

In general I have a hard time understanding how WordPress selects which template(route) to take.

The Template Hierarchy answers all of these questions. :)

I may have misunderstood, but I expect WordPress to determine which template to use before it starts interpreting its contents.

It certainly does.

At that point it can display anything you want depending on its contents and using the is_sometype() functions.

Sure, you can; but if you're using/loading a more-specific, context-based template file, you don't need to. For example, if you're loading date.php, you don't need to use the is_date() conditional. It's already known to be true, if that template file is loaded.

What I don't understand is how it reaches that point. Have I got this reasoning wrong? I'd be very grateful if someone could address this point.

The place to start is the query itself, which contains/defines the current context:

  1. Build the query
  2. Within the query, define current context
  3. Based on current context, determine which template file to load

I have a more-advanced version of the Template Hierarchy diagram, that incorporates/correlates all of the context-based conditionals. It might help with understanding the process.

Edit

To address your additional questions:

Do I understand it correctly that WP parses permalinks using rewrite rules to build the query, or else the query is already in "native" format, i.e. a URL followed by ? and a concatenation of query variables? Can a combination of both occur?

Generally speaking, rewrite rules are a separate (though related) topic. If you have specific questions about rewrite rules, I would search the site, and or ask a separate question.

That said: the rewrite rules, just like the context/template, are derived from the query. WordPress determines the contextually based template whether or not rewrite rules are used.

What is the definition of "current context" with regard to queries? Could you give an example?

Again, you have to go back to the query. The query is an object that determines/defines/holds all of the contextual information. Have a look at the WP_Query() class in source, if you want the gritty details.

Essentially, context is determined by URL query variables. (Note: this is not about pretty permalinks or rewrite rules, which also use the same, underlying URL query variables.) You can see the query variable parsing in action here.

What do the blue .keywords signify that are below each box in Chip's diagram?

Those are CSS classes added to body_class(), based on context.

3
  • I had a feeling I was approaching it backwards so I'm very grateful for any input to straighten it out.
    – ivvi
    May 23, 2012 at 12:06
  • Thank you very much for taking the time to give such a thorough answer. It was actually your diagram that I was referring to when I wrote about the "notes" surrounding boxes. I have added a few follow-up question to my original question.
    – ivvi
    May 23, 2012 at 12:15
  • I've updated my answer, to address your additional questions. :) May 23, 2012 at 13:45
0
           1. category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php
           2. category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php
           3. category.php
           4. archive.php
           5. index.php 

This should work.Let me know if you need to know more.

1
  • I elaborated in my question.
    – ivvi
    May 22, 2012 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.