{"id":263,"date":"2013-09-23T16:41:55","date_gmt":"2013-09-23T14:41:55","guid":{"rendered":"http:\/\/hesmid.nl\/test\/?p=263"},"modified":"2013-09-24T00:26:33","modified_gmt":"2013-09-23T22:26:33","slug":"the-wordpress-template-hierarchy","status":"publish","type":"post","link":"https:\/\/hesmid.nl\/test\/the-wordpress-template-hierarchy\/","title":{"rendered":"The WordPress Template Hierarchy"},"content":{"rendered":"<p><a href=\"http:\/\/codex.wordpress.org\/Template_Hierarchy\">http:\/\/codex.wordpress.org\/Template_Hierarchy<\/a><\/p>\n<h3>The General Idea<\/h3>\n<p>WordPress uses the\u00a0<a title=\"Glossary\" href=\"http:\/\/codex.wordpress.org\/Glossary#Query_String\">Query String<\/a>\u00a0\u2014 information contained within each link on your web site \u2014 to decide which template or set of templates will be used to display the page.<\/p>\n<p>To see the query string put this in your theme&#8217;s page template:<\/p>\n<pre><code>&lt;?php\r\necho \"&lt;pre&gt;\"; print_r($wp_query-&gt;query_vars); echo \"&lt;\/pre&gt;\";\r\n?&gt;<\/code><\/pre>\n<p>WordPress matches every Query String to query types \u2014 i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.<\/p>\n<p>Templates are then chosen \u2014 and web page content is generated \u2014 in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.<!--more--><\/p>\n<p>WordPress looks for template files with specific names in the current Theme&#8217;s directory and uses the\u00a0<i>first matching<\/i>\u00a0template file listed under the appropriate query section below.<\/p>\n<p>With the exception of the basic\u00a0<tt>index.php<\/tt>\u00a0template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file,\u00a0<tt>index.php<\/tt>\u00a0(the Theme&#8217;s home page template file) will be used.<\/p>\n<h3>Examples<\/h3>\n<p>If your blog is at\u00a0<tt>http:\/\/example.com\/blog\/<\/tt>\u00a0and a visitor clicks on a link to a category page like\u00a0<tt>http:\/\/example.com\/blog\/category\/your-cat\/<\/tt>: Here is the progression of how WordPress uses the template hierarchy to find and generate the right file.<\/p>\n<p>WordPress looks for a template file in the current Theme&#8217;s directory that matches the category&#8217;s ID.<\/p>\n<ol>\n<li>If the category&#8217;s ID is 4, WordPress looks for a template file named\u00a0<tt>category-4.php<\/tt>.<\/li>\n<li>If it is missing, WordPress next looks for a generic category template file,\u00a0<tt>category.php<\/tt>.<\/li>\n<li>If this file does not exist either, WordPress looks for a generic archive template,\u00a0<tt>archive.php<\/tt>.<\/li>\n<li>If it is missing as well, WordPress falls back on the main Theme template file,\u00a0<tt>index.php<\/tt>.<\/li>\n<\/ol>\n<p><span style=\"font-family: Bitter, Georgia, serif; font-size: 20px; line-height: 1.3;\">Page display<\/span><\/p>\n<p>Template file used to render a static page (<tt>page<\/tt>\u00a0post-type)<\/p>\n<dl>\n<dd>\n<ol>\n<li><tt>custom template file<\/tt>\u00a0&#8211; The\u00a0<a title=\"Page Templates\" href=\"http:\/\/codex.wordpress.org\/Page_Templates\">Page Template<\/a>\u00a0assigned to the Page. See\u00a0<a title=\"Class Reference\/WP Theme\" href=\"http:\/\/codex.wordpress.org\/Class_Reference\/WP_Theme#Get_Custom_Page_Templates\">get_page_templates()<\/a>.<\/li>\n<li><tt>page-{slug}.php<\/tt>\u00a0&#8211; If the page slug is\u00a0<b>recent-news<\/b>, WordPress will look to use\u00a0<tt>page-recent-news.php<\/tt><\/li>\n<li><tt>page-{id}.php<\/tt>\u00a0&#8211; If the page ID is\u00a0<b>6<\/b>, WordPress will look to use\u00a0<tt>page-6.php<\/tt><\/li>\n<li><tt>page.php<\/tt><\/li>\n<li><tt>index.php<\/tt><\/li>\n<\/ol>\n<figure class=\"thumbnail wp-caption alignnone\" style=\"width: 1463px\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/codex.wordpress.org\/images\/1\/18\/Template_Hierarchy.png\" width=\"1453\" height=\"1443\" \/><figcaption class=\"caption wp-caption-text\"><a href=\"http:\/\/codex.wordpress.org\/images\/1\/18\/Template_Hierarchy.png\">Template_Hierarchy<\/a><\/figcaption><\/figure>\n<\/dd>\n<\/dl>\n<h3>Using Conditional Tags<\/h3>\n<p>WordPress provides more than one way to match templates to query types. WordPress Theme developers can also use\u00a0<a title=\"Theme Development\" href=\"http:\/\/codex.wordpress.org\/Theme_Development#Query-based_Template_Files\">Conditional Tags<\/a>\u00a0to control which templates will be used to generate a certain page. Some WordPress Themes may not implement all of the template files described here. Some Themes use conditional tags to load other template files.<\/p>\n<p>WordPress can load different\u00a0<a title=\"Stepping Into Templates\" href=\"http:\/\/codex.wordpress.org\/Stepping_Into_Templates\">Templates<\/a>\u00a0for different\u00a0<i>query<\/i>\u00a0types. There are two ways to do this: as part of the built-in\u00a0<a title=\"Template Hierarchy\" href=\"http:\/\/codex.wordpress.org\/Template_Hierarchy\">Template Hierarchy<\/a>, and through the use of\u00a0<a title=\"Conditional Tags\" href=\"http:\/\/codex.wordpress.org\/Conditional_Tags\">Conditional Tags<\/a>\u00a0within\u00a0<a title=\"The Loop\" href=\"http:\/\/codex.wordpress.org\/The_Loop\">The Loop<\/a>\u00a0of a template file.<\/p>\n<p>To use the\u00a0<a title=\"Template Hierarchy\" href=\"http:\/\/codex.wordpress.org\/Template_Hierarchy\">Template Hierarchy<\/a>, you basically need to provide special-purpose Template files, which will automatically be used to override\u00a0<i>index.php<\/i>. For instance, if your Theme provides a template called\u00a0<i>category.php<\/i>\u00a0and a category is being queried,\u00a0<i>category.php<\/i>will be loaded instead of\u00a0<i>index.php<\/i>. If\u00a0<i>category.php<\/i>\u00a0is not present,\u00a0<i>index.php<\/i>\u00a0is used as usual.<\/p>\n<p>You can get even more specific in the Template Hierarchy by providing a file called, for instance,\u00a0<i>category-6.php<\/i>\u00a0&#8212; this file will be used rather than\u00a0<i>category.php<\/i>\u00a0when generating the page for the category whose ID number is 6.<\/p>\n<p>If your Theme needs to have even more control over which Template files are used than what is provided in the\u00a0<a title=\"Template Hierarchy\" href=\"http:\/\/codex.wordpress.org\/Template_Hierarchy\">Template Hierarchy<\/a>, you can use\u00a0<a title=\"Conditional Tags\" href=\"http:\/\/codex.wordpress.org\/Conditional_Tags\">Conditional Tags<\/a>. The Conditional Tag basically checks to see if some particular condition is true, within the\u00a0<a title=\"The Loop in Action\" href=\"http:\/\/codex.wordpress.org\/The_Loop_in_Action\">WordPress Loop<\/a>, and then you can load a particular template, or put some particular text on the screen, based on that condition.<\/p>\n<p>For example, to generate a distinctive stylesheet in a post only found within a specific category, the code might look like this:<\/p>\n<pre>&lt;?php\r\nif ( is_category( '9' ) ) {\r\n    get_template_part( 'single2' ); \/\/ looking for posts in category with ID of '9'\r\n} else {\r\n    get_template_part( 'single1' ); \/\/ put this on every other category post\r\n}\r\n?&gt;<\/pre>\n<p>Or, using a query, it might look like this:<\/p>\n<pre>&lt;?php\r\n$post = $wp_query-&gt;post;\r\nif ( in_category( '9' ) ) {\r\n    get_template_part( 'single2' );\r\n} else {\r\n    get_template_part( 'single1' );\r\n}\r\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>http:\/\/codex.wordpress.org\/Template_Hierarchy The General Idea WordPress uses the\u00a0Query String\u00a0\u2014 information contained within each link on your web site \u2014 to decide which template or set of templates will be used to display the page. To see the query string put this in your theme&#8217;s page template: &lt;?php echo &#8220;&lt;pre&gt;&#8221;; print_r($wp_query-&gt;query_vars); echo &#8220;&lt;\/pre&gt;&#8221;; ?&gt; WordPress matches every [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/263"}],"collection":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":8,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":271,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/posts\/263\/revisions\/271"}],"wp:attachment":[{"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hesmid.nl\/test\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}