{"id":1714,"date":"2015-06-28T17:38:43","date_gmt":"2015-06-28T16:38:43","guid":{"rendered":"http:\/\/www.jurecuhalev.com\/blog\/?p=1714"},"modified":"2015-06-28T17:42:52","modified_gmt":"2015-06-28T16:42:52","slug":"architecting-scalable-wordpress-themes-that-are-easy-to-maintain","status":"publish","type":"post","link":"https:\/\/www.jurecuhalev.com\/blog\/architecting-scalable-wordpress-themes-that-are-easy-to-maintain\/","title":{"rendered":"Architecting scalable WordPress  Themes that are easy to maintain"},"content":{"rendered":"<p>WordPress Themes are like good computer games &#8211; easy to learn, but difficult to master (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bushnell's_Law\">Bushnell&#8217;s Law<\/a>). Unless you start writing them from scratch, you don\u2019t appreciate good structure.<\/p>\n<p>WordPress Template Hierarchy diagram displays all the possibilities\u00a0we have:<\/p>\n<p><a href=\"https:\/\/developer.wordpress.org\/themes\/basics\/template-hierarchy\/\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1715\" src=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/wp-template-hierarchy-1024x640.jpg\" alt=\"wp-template-hierarchy\" width=\"604\" height=\"378\" srcset=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/wp-template-hierarchy-1024x640.jpg 1024w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/wp-template-hierarchy-550x344.jpg 550w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>While it explains, how things are working, it also shows how many different templates we need to create.<\/p>\n<p>There is an alternative approach &#8211; we can use <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/get_template_part\">get_template_part<\/a>. That way the core of your theme\u00a0resides in <em>index.php<\/em> and specific template partials.<\/p>\n<p>Your index.php should like this:<\/p>\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/gandalfar\/bce4d54af4a76896ef78.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/gandalfar\/bce4d54af4a76896ef78\">Gist<\/a>.<\/noscript><\/div>\n<p>or more high level:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-1719\" src=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/File-28-06-15-18-27-42-1024x811.png\" alt=\"File 28-06-15 18 27 42\" width=\"604\" height=\"478\" srcset=\"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/File-28-06-15-18-27-42-1024x811.png 1024w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/File-28-06-15-18-27-42-550x435.png 550w, https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/File-28-06-15-18-27-42.png 1441w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<p>For each custom post type, create a template part inside <em>partials\/content-{post-type}.php<\/em>. If it doesn&#8217;t exist, WordPress will load <em>partials\/content.php<\/em>.<\/p>\n<p>This approach has several benefits:<\/p>\n<ul>\n<li>You don\u2019t have to repeat the header and loop code for every template, reducing the amount of code repetition.<\/li>\n<li>You can reuse template parts in other parts of the side (e.g. search results or custom archive pages).<\/li>\n<li>If you write good generic fallback for template partials, you can have a first prototype of the theme running much faster.<\/li>\n<\/ul>\n<p>Excellent starter theme\u00a0that follows this design is <a href=\"https:\/\/github.com\/Automattic\/_s\">_s (underscores) theme<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress Themes are like good computer games &#8211; easy to learn, but difficult to master (Bushnell&#8217;s Law). Unless you start writing them from scratch, you don\u2019t appreciate good structure. WordPress Template Hierarchy diagram displays all the possibilities\u00a0we have: While it explains, how things are working, it also shows how many different templates we need to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1717,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[924],"tags":[],"class_list":["post-1714","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-tutorial"],"acf":[],"jetpack_featured_media_url":"https:\/\/www.jurecuhalev.com\/blog\/wp-content\/uploads\/2015\/06\/5896490052_7c3b0da949_b.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/comments?post=1714"}],"version-history":[{"count":3,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1714\/revisions"}],"predecessor-version":[{"id":1721,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/posts\/1714\/revisions\/1721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media\/1717"}],"wp:attachment":[{"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/media?parent=1714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/categories?post=1714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jurecuhalev.com\/blog\/wp-json\/wp\/v2\/tags?post=1714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}