{"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":"\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\">This article will cover everything you need to know about <strong>get_template_part()<\/strong> \u2012 what it is, how it works, and how to use it effectively. We\u2019ll also highlight common mistakes to avoid along the way.<\/p>\n\n\n\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-what-is-get-template-part\"><strong>What is get_template_part()<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>get_template_part()<\/strong> function loads reusable sections of your theme template.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using this <a data-click-id=\"hgr-post-content-link-wordpress-function\" href=\"\/in\/tutorials\/functions-php-wordpress\/\">WordPress function<\/a> helps keep your theme\u2019s code clean and organized. Updates also become simpler \u2012 make changes in one file, and they automatically update everywhere the template part is used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a team environment, this allows developers to work on different parts of a theme at the same time without overwriting each others\u2019 work. Plus, this function works with child themes, allowing for customization without modifying the parent theme\u2019s files.<\/p>\n\n\n<figure class=\"wp-block-image size-large\"><a data-click-id=\"hgr-post-content-link-in-wordpress-hosting\" aria-label=\"Wordpress hosting\" href=\"\/in\/wordpress-hosting\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/52\/2024\/06\/New-WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-understanding-the-relationship-between-themes-and-template-parts\"><strong>Understanding the relationship between themes and template parts<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In WordPress, themes control your site&#8217;s design and overall structure, while template parts are smaller <a data-click-id=\"hgr-post-content-link-php-files\" href=\"\/in\/tutorials\/what-is-php\/\">PHP files<\/a> containing reusable sections of code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-how-to-use-the-get-template-part-function\"><strong>How to use the get_template_part() function<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here are various ways of using the WordPress template tag, depending on your theme\u2019s structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" 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>\n\n\n\n<p class=\"wp-block-paragraph\">The most basic way to use <strong>get_template_part()<\/strong> is to include a file name, like this:<\/p>\n\n\n\n<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>\n\n\n\n<p class=\"wp-block-paragraph\">This method works best for incorporating template parts like headers, footers, and sidebars that are used often and don\u2019t need customization for specific conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you include the following syntax in a page, it will include <strong>header.php<\/strong> from your theme\u2019s root directory.<\/p>\n\n\n\n<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>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" 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>\n\n\n\n<p class=\"wp-block-paragraph\">If the template part is stored in one of your theme\u2019s subdirectories, add a parameter to the function as follows:<\/p>\n\n\n\n<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>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<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>\n\n\n\n<h3 class=\"wp-block-heading h-t-title-3\" id=\"h-calling-fallback-files-using-the-get-template-part-function\"><strong>Calling fallback files using the get_template_part() function<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;t exist.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simply add parameters to the core function like this:<\/p>\n\n\n\n<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>\n\n\n\n<p class=\"wp-block-paragraph\">The first parameter (&#8216;<strong>default-file<\/strong>&#8216;) specifies the base name of the template part to load, while the second parameter (&#8216;<strong>target-file<\/strong>&#8216;) serves as the suffix. WordPress will combine these two parameters and look for a file named <strong>default-file-target-file.php<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If that file is missing, WordPress will fall back to the base name file instead of returning nothing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example:<\/p>\n\n\n\n<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>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress first looks for<strong> content-post.php<\/strong> in the <strong>template-parts <\/strong>folder. If it doesn\u2019t exist, WordPress automatically falls back and loads <strong>template-parts\/content.php<\/strong> from the same folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-common-mistakes-in-using-the-get-template-part-function\"><strong>Common mistakes in using the get_template_part() function<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Incorrect file path<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Misplacing template parts or failing to specify the correct file path can cause WordPress to think the file doesn\u2019t exist. This usually happens when fetching template parts in folders.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check whether your code points to the right directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Incorrect file naming conventions<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t name your files correctly, WordPress won\u2019t be able to find them. Make sure your file names match exactly, especially when using additional parameters like &#8216;post&#8217; or &#8216;page.&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Not using fallback files when needed<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Your site can break if a template part is missing, so it\u2019s 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\u2019t locate <strong>content-post.php<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Fetching non-reusable code<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you\u2019re only using something once, don\u2019t overcomplicate it by creating a template part. Only use the function for major sections like headers and footers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Not considering child themes<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When using <strong>get_template_part()<\/strong> in the parent theme, WordPress will first look for the template part in the child theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to override a template part, do so in the child theme\u2019s 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\u2019t work out.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check out our article on <a data-click-id=\"hgr-post-content-link-creating-a-wordpress-child-theme\" 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>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using the <strong>get_template_part()<\/strong> function makes WordPress theme development easier. It\u2019s also helpful for keeping your code lean and organized, which could benefit site performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s easy to use and quite flexible, so give it a try in your next project and see how it enhances your workflow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading h-t-title-2\" id=\"h-get-template-part-function-faq\"><strong>get_template_part() function FAQ<\/strong><\/h2>\n\n\n\n<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?<\/h3><\/strong> <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&#8217;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?<\/h3><\/strong> <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?<\/h3><\/strong> <p class=\"schema-faq-answer\">Using <strong>get_template_part()<\/strong> doesn\u2019t impact performance much since it helps organize your theme into smaller parts. However, it\u2019s best to avoid overusing it \u2013 or misusing it on template parts that will only be used once \u2013 and keep your template parts simple. And don\u2019t forget to <a data-click-id=\"hgr-post-content-link-enable-caching\" 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":"","_content_updated_date":"","footnotes":""},"categories":[22668,22667],"tags":[],"class_list":["post-126254","post","type-post","status-publish","format-standard","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-get-template-part-wordpress-function\/","default":1},{"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-GB","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-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}]}}