{"id":126254,"date":"2025-03-31T08:42:53","date_gmt":"2025-03-31T08:42:53","guid":{"rendered":"\/tutorials\/?p=126254"},"modified":"2025-04-23T08:30:14","modified_gmt":"2025-04-23T08:30:14","slug":"get_template_part","status":"publish","type":"post","link":"\/in\/tutorials\/get_template_part","title":{"rendered":"What is the get_template_part function and how to use it in WordPress\u00a0"},"content":{"rendered":"<p>Knowing how to customize your WordPress theme is a crucial step in shaping your site to match your vision. Key to helping you achieve this is the <strong>get_template_part()<\/strong> function, which allows you to embed parts of your template in various locations without duplicating code.<\/p><p>This article will cover everything you need to know about <strong>get_template_part()<\/strong> &#8210; what it is, how it works, and how to use it effectively. We&rsquo;ll also highlight common mistakes to avoid along the way.<\/p><p>\n\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-what-is-get-template-part\"><strong>What is get_template_part()<\/strong><\/h2><p>The <strong>get_template_part()<\/strong> function loads reusable sections of your theme template.<\/p><p>Using this <a href=\"\/in\/tutorials\/functions-php-wordpress\">WordPress function<\/a> helps keep your theme&rsquo;s code clean and organized. Updates also become simpler &#8210; make changes in one file, and they automatically update everywhere the template part is used.<\/p><p>In a team environment, this allows developers to work on different parts of a theme at the same time without overwriting each others&rsquo; work. Plus, this function works with child themes, allowing for customization without modifying the parent theme&rsquo;s files.<\/p><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a href=\"\/in\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner-1024x300.png\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1024x300.png 1024w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-300x88.png 300w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-150x44.png 150w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-768x225.png 768w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner-1536x450.png 1536w, https:\/\/www.hostinger.com\/in\/tutorials\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h3 class=\"wp-block-heading\" id=\"h-understanding-the-relationship-between-themes-and-template-parts\"><strong>Understanding the relationship between themes and template parts<\/strong><\/h3><p>In WordPress, themes control your site&rsquo;s design and overall structure, while template parts are smaller <a href=\"\/in\/tutorials\/what-is-php\/\">PHP files<\/a> containing reusable sections of code.<\/p><p>A theme will include various templates that display content, but most pages share common elements like headers and footers. These elements are called template parts.<\/p><p>Instead of duplicating code across multiple files, you can move template parts into a separate file and use <strong>get_template_part() <\/strong>to call them whenever needed.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-get-template-part-function\"><strong>How to use the get_template_part() function<\/strong><\/h2><p>Here are various ways of using the WordPress template tag, depending on your theme&rsquo;s structure.<\/p><h3 class=\"wp-block-heading\" id=\"h-using-the-get-template-part-function-to-call-template-parts\"><strong>Using the get_template_part() function to call template parts<\/strong><\/h3><p>The most basic way to use <strong>get_template_part()<\/strong> is to include a file name, like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'file-name' );<\/pre><p>This method works best for incorporating template parts like headers, footers, and sidebars that are used often and don&rsquo;t need customization for specific conditions.<\/p><p>For example, if you include the following syntax in a page, it will include <strong>header.php<\/strong> from your theme&rsquo;s root directory.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'header' );<\/pre><h3 class=\"wp-block-heading\" id=\"h-calling-template-parts-within-a-directory-using-the-get-template-part-function\"><strong>Calling template parts within a directory using the get_template_part() function<\/strong><\/h3><p>If the template part is stored in one of your theme&rsquo;s subdirectories, add a parameter to the function as follows:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'folder-name\/file-name' );<\/pre><p>For example, the syntax below tells WordPress to look for a file named <strong>content.php<\/strong> inside the <strong>template-parts<\/strong> directory of your theme.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'template-parts\/content' );<\/pre><h3 class=\"wp-block-heading\" id=\"h-calling-fallback-files-using-the-get-template-part-function\"><strong>Calling fallback files using the get_template_part() function<\/strong><\/h3><p>This method is useful when you want to call a preferred version of a template part, but you also want a backup version in case the first one doesn&rsquo;t exist.<\/p><p>Simply add parameters to the core function like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'default-file', 'target-file' );<\/pre><p>The first parameter (&lsquo;<strong>default-file<\/strong>&lsquo;) specifies the base name of the template part to load, while the second parameter (&lsquo;<strong>target-file<\/strong>&lsquo;) serves as the suffix. WordPress will combine these two parameters and look for a file named <strong>default-file-target-file.php<\/strong>.<\/p><p>If that file is missing, WordPress will fall back to the base name file instead of returning nothing.<\/p><p>Here&rsquo;s an example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">get_template_part( 'template-parts\/content', 'post' );<\/pre><p>WordPress first looks for<strong> content-post.php<\/strong> in the <strong>template-parts <\/strong>folder. If it doesn&rsquo;t exist, WordPress automatically falls back and loads <strong>template-parts\/content.php<\/strong> from the same folder.<\/p><h2 class=\"wp-block-heading\" id=\"h-common-mistakes-in-using-the-get-template-part-function\"><strong>Common mistakes in using the get_template_part() function<\/strong><\/h2><p>The <strong>get_template_part()<\/strong> function is a powerful tool in WordPress, but it can present challenges. Here are some common issues users often face when working with this function:<\/p><p><strong>Incorrect file path<\/strong><\/p><p>Misplacing template parts or failing to specify the correct file path can cause WordPress to think the file doesn&rsquo;t exist. This usually happens when fetching template parts in folders.<\/p><p>Check whether your code points to the right directory.<\/p><p><strong>Incorrect file naming conventions<\/strong><\/p><p>If you don&rsquo;t name your files correctly, WordPress won&rsquo;t be able to find them. Make sure your file names match exactly, especially when using additional parameters like &lsquo;post&rsquo; or &lsquo;page.&rsquo;<\/p><p><strong>Not using fallback files when needed<\/strong><\/p><p>Your site can break if a template part is missing, so it&rsquo;s crucial to have a backup file in place. For example, using <strong>content.php<\/strong> as a fallback ensures WordPress has a default option if it can&rsquo;t locate <strong>content-post.php<\/strong>.<\/p><p><strong>Fetching non-reusable code<\/strong><\/p><p>If you&rsquo;re only using something once, don&rsquo;t overcomplicate it by creating a template part. Only use the function for major sections like headers and footers.<\/p><p><strong>Not considering child themes<\/strong><\/p><p>When using <strong>get_template_part()<\/strong> in the parent theme, WordPress will first look for the template part in the child theme.<\/p><p>If you want to override a template part, do so in the child theme&rsquo;s PHP file and leave the default version in the parent theme as is. This way, you can easily roll back if you change your mind or things don&rsquo;t work out.<\/p><p>Check out our article on <a href=\"\/in\/tutorials\/how-to-create-wordpress-child-theme\">creating a WordPress child theme<\/a> to learn how child themes work, and to find out how to set one up properly.<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\"><strong>Conclusion<\/strong><\/h2><p>Using the <strong>get_template_part()<\/strong> function makes WordPress theme development easier. It&rsquo;s also helpful for keeping your code lean and organized, which could benefit site performance.<\/p><p>It&rsquo;s easy to use and quite flexible, so give it a try in your next project and see how it enhances your workflow.<\/p><p>We hope this article helped you understand how to make the most out of <strong>get_template_part()<\/strong>. Got any questions? Drop us a comment below.<\/p><h2 class=\"wp-block-heading\" id=\"h-get-template-part-function-faq\"><strong>get_template_part() function FAQ<\/strong><\/h2><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1743410422377\"><h3 class=\"schema-faq-question\"><strong>What are the benefits of using get_template_part?<\/strong><\/h3> <p class=\"schema-faq-answer\">The <strong>get_template_part()<\/strong> function keeps your theme organized by avoiding duplicated code. Changes made in one template part will be up to date wherever it&rsquo;s used. Additionally, it makes it easier for developers to work on different sections at the same time, and lets you customize child themes without changing the parent theme.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1743410427273\"><h3 class=\"schema-faq-question\"><strong>How do I load multiple template parts using this function?<\/strong><\/h3> <p class=\"schema-faq-answer\">The simplest way to load multiple template parts with <strong>get_template_part()<\/strong> is to use the function multiple times in your template file, each time for a different section. WordPress will then load the appropriate part for each call.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1743410433558\"><h3 class=\"schema-faq-question\"><strong>Is there a performance impact when using get_template_part?<\/strong><\/h3> <p class=\"schema-faq-answer\">Using <strong>get_template_part()<\/strong> doesn&rsquo;t impact performance much since it helps organize your theme into smaller parts. However, it&rsquo;s best to avoid overusing it &ndash; or misusing it on template parts that will only be used once &ndash; and keep your template parts simple. And don&rsquo;t forget to <a href=\"\/in\/tutorials\/website-cache\">enable caching<\/a> to optimize loading speed.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Knowing how to customize your WordPress theme is a crucial step in shaping your site to match your vision. Key to helping you achieve this is the get_template_part() function, which allows you to embed parts of your template in various locations without duplicating code. This article will cover everything you need to know about get_template_part() [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/in\/tutorials\/get_template_part\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-126254","post","type-post","status-publish","format-standard","hentry"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/get_template_part","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/get_template_part","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-use-get-template-part-wordpress-function","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/126254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/comments?post=126254"}],"version-history":[{"count":3,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/126254\/revisions"}],"predecessor-version":[{"id":126276,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/posts\/126254\/revisions\/126276"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/media?parent=126254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/categories?post=126254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-json\/wp\/v2\/tags?post=126254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}