NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Easy
Web Hosting Canada

Last modified : Sep 03 2022

Estimated reading time : 1 minute, 16 seconds - 184 words

In this tutorial, we will show you how to use the get_template_part in your WordPress templates in order to logically structure things in our WordPress theme.

The get_template_part() function allows users to call a file in multiple templates at once in order to simplify dynamic integration.

What can we use with the get_template_part() function?

Breadcrumbs, Archive titles, filters, loop posts, etc…

In your theme’s root folder, we will need a new folder called template-part. This new folder will contain all necessary files that will be executed by the get_template_part() function.

In the example below, we will use the get_template_part() function for our Breadcrumbs with the Yoast SEO breadcrumbs.

<?php if(function_exists('yoast_breadcrumb')) { ?>
  <div id="breadcrumbs" class="bg-light py-3 text-muted">
    <div class="container">
      <?php yoast_breadcrumb(); ?>
    </div>
  </div>
<?php } ?>

In your template-part folder, create a new breadcrumbs.php file and add the code snippet below:

In your desired template, you’ll be able to call the file for your breadcrumbs.

<?php get_template_part( 'template-part/breadcrumbs' ); ?>

As you might have noticed, the get_template_part() contains the folder name (template-part), and then the file name(breadcrumbs). You can call it in all templates necessary.

Learn more about the get_template_part() function

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>