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
If you look at the template hierarchy for categories you will see it follows:
- category-{slug}.php
- category-{id}.php
- category.php
- archive.php
- 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.