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 am curious how WordPress know that if I have a category.php and do the basic loop inside there that when I click on category A I get all of A's posts. The reason I ask, is because I have seen themes that just have an index.php and when you click on Category A in those themes you get all of A's posts, and they do not have a category.php file.

Is there a trick to achieve this?

because if you do:

category.php and inside that do:

if(have_posts()){
    while(have_posts()){
        the_post();
    }
}

You will get all posts for any category link you click on. How ever, as stated some people only have index.php in their theme and when you click on a category link you get all posts for that category, regardless of category clicked on.

Much like global post_id is there a global category_id to query against?

share|improve this question

3 Answers 3

up vote 0 down vote accepted

If you look at the template hierarchy for categories you will see it follows:

  1. category-{slug}.php
  2. category-{id}.php
  3. category.php
  4. archive.php
  5. index.php

As Milo says these templates are just using the loop to display what is already queried, so there template itself does not matter outside load order of the hierarchy.

share|improve this answer
    
Again, this does not answer my question. As stated some people do not have any variation of category.php in their theme as a template any where, yet clicking on a category renderes a page with all that category's posts –  Kyle Adams Feb 18 '13 at 0:55
    
You need to have one of those 5 files present, as shown index.php is the last fallback. The template file does not render the query as Milo stated, it only outputs what the query is, if you want a more detailed answer on how the WordPress query works then state so in your question. In essence the template does not care, it only works with the data available. –  Wyck Feb 18 '13 at 1:01
    
so I do not, need a category.php file. Just a index.php. and then I can do a check and say if is category do this, eslse do that –  Kyle Adams Feb 18 '13 at 1:02
    
That is why it is listed as 1, 2, 3, 4, 5 , those are real numbers and have real meaning in the hierarchy, and yes you only need an index.php to render all WordPress content –  Wyck Feb 18 '13 at 1:03

Index.php defines how to display a loop of posts

category.php does the same thing but it's included when displaying a category archive

WordPress will look for the most specific template it can find, and work backwards until it finds a file that exists, where index.php is the most generic.

So long as the template implements the main loop, it will function correctly. Which template is used has no bearing on the kind of post displayed. Templates are purely visual/aesthetic

The actual fetching of the posts, be they category posts, a single individual post, a page, a date archive etc, is done using query vars, and is done long before WordPress picks which template to use.

  • Your rewrite rules will run some regex to extract some bits of data from the URL.
  • This data is then sanitised and used as query variables ( the details of which you can see in the codex WP_Query page )
  • Based on these query variables, checks are made, and internal values such as 'is_main_loop' and 'is_search' etc are filled in.
  • Now that the query vars are done, we have all we need to execute the main loop, so it is instantiated and setup ready
  • Based on the checks earlier, the template loader now runs. It refers to the main query object, and follows the template hierarchy
  • The most 'specific' template found is loaded. If this template has implemented the main loop ( as it should do ), the posts loaded earlier are now displayed.
  • If the most specific template isn't found, it falls back to the next most specific, and repeats until it reaches the most 'generic' template aka index.php. index.php is required for a theme to work so it will always be present.

There is no difference between loading index.php and category.php or archive.php etc.

So why do we have category.php? Well we only need index.php, but maybe we want our category archives to look different from our main listing? Maybe we want the site to look different for listings from individual posts? Rather than having a big if else setup in index.php e.g. if listing do this, else if category listing do that, etc WordPress has the template heirarchy.

Templates are purely about visual and html markup. They are presentation, and have nothing to do with what the data is or where the data comes from.

See this handy flowchart to see what WordPress will use:

enter image description here

Finally an analogy to demonstrate your questions problem:

You're running a company that makes tables, square tables, and employ 5 carpenters and a designer who likes square tables.

One day you hire a new designer and a carpenter. This new designer likes circular tables. You notice that after giving the new carpenter these designs that he produces circular tables. What is so special that this carpenter makes circular tables, when the others will make circular tables too if I fire him?

share|improve this answer
    
This does not answer my question. As stated some people do not have any other files other then index.php, functions.php and style.css and yet if you click on a category link you get that categories posts. How is this done with out a categoy.php file? –  Kyle Adams Feb 18 '13 at 0:53
    
It does explain it. There's nothing special about category.php, WordPress looks for a template file, the only thing it needs is a template with a post loop. Category.php is just loaded if it's present, it doesn't 'do' anything special. –  Tom J Nowell Feb 18 '13 at 9:52

The request is parsed and the database is queried before the template is loaded. The loop simply iterates over the post results, whatever they may be. The template is selected based on the results of the request, the contents of the template have no bearing on what the main query contains.

To get a better understanding of how it all works, I suggest looking at the Action Reference in Codex and searching each action in source to see what WordPress is doing before the template is loaded. Particularly parse_request and parse_query.

share|improve this answer
    
again some people do not have these templates in their theme - yet clicking on a category will shows you all posts for that category. Again they do not have category.php or any various of said template in their theme –  Kyle Adams Feb 18 '13 at 0:55
    
the template has nothing to do with what posts are queried. the incoming request tells WordPress what to query from the database. Based on what type of query it is, WordPress then tries to load the appropriate template, starting at the most specific and going down the hierarchy toward the most general until it finds a template that exists. all the template does is output the posts in the query object, which exists before WP tries to load a template. –  Milo Feb 18 '13 at 1:02

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.