understanding theme hierarchy

I know there is a high chance I understood this the wrong way and I have a linear thinking so please bear with me, I am just following the official documentation

my understanding is if I create a file test.php for example in the root directory of my theme.

and I visit the link to

example.com/test.php

or just

example.com/test

wordpress will follow the hierarchy rules and look for the file test.php and show me its content

however that doesn't happen.

I always get what's in index.php

my question is: why ?

thank you

Topic template-hierarchy themes Wordpress

Category Web


Your understanding is incorrect. That's not how WordPress works, and that documentation is not saying what you describe.

The hierarchy rules tell you what template is used when you view content types created in WordPress, such as Pages and Posts.

If you create a page called "Test" in Pages > Add New, then when you visit that page's URL it will use the hierarchy to determine which file to use as its template. For a page called "Test" these are the files it will try:

  • page-test.php, if it exists.
  • page-123.php, if it exists (in this example the page ID is 123).
  • page.php, if it exists.
  • singular.php, if it exists.
  • index.php

You do not create content in the theme files.

WordPress is a content management system. It is not just pointing URLs to files in your theme. You use its interface (or REST API) to create content which is saved in the database. When you visit a URL, WordPress uses your permalink settings (Settings > Permalinks) to determine the type of content you're viewing, queries the correct posts from the database, and then displays the result using the PHP templates in your theme by following the template hierarchy.


Here there is a complete hierarchy map Hyerarchy Map visualization as you can see there's a lot involving which template is Wordpress loading. If you want to load a specific template in a page you can follow the instructions on the Wordpress theme developer handbook found in here: Page templates handbook .

I recommend you to add this code in your functions.php file in order to know which specific file you are rendering:

function cagb_which_template_is_loaded() {
    if ( is_super_admin() ) {
        global $template;
        print_r( $template );
    }
}
 
add_action( 'wp_footer', 'cagb_which_template_is_loaded' );

If you are logged in as admin, this will add (at the bottom of the page) the route of the theme file that Wordpress is rendering.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.