Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
did you checked template hierarchy? –  Sisir May 22 '12 at 9:20
    
@Sisir post it as an answer –  Eugene Manuilov May 22 '12 at 9:38
    
@Sisir please post answers as answers, rather than as comments. :) –  Chip Bennett May 22 '12 at 14:22

2 Answers 2

           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.

share|improve this answer
    
I elaborated in my question. –  seron May 22 '12 at 13:53

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.

share|improve this answer
    
I had a feeling I was approaching it backwards so I'm very grateful for any input to straighten it out. –  seron May 23 '12 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. –  seron May 23 '12 at 12:15
    
I've updated my answer, to address your additional questions. :) –  Chip Bennett May 23 '12 at 13:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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