wordpress loop and template files

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?

3 Answers
3

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.

Leave a Comment