Yogesh Chauhan's Blog

How get_template_part helps write reusable code in WordPress?

in WordPress on July 25, 2021

get_template_part used to load a part of a template file into the template and helps divide the large file into smaller templates.

That’s the beauty of get_template_part. What you can do is save small chunks of code in a separate folder that can be used on multiple templates and then load those reusable components.

The template is included using PHP require which lets import same file multiple times on a page.

Syntax


get_template_part( $slug, $name, $args )

Where

  • $slug is the slug name of the template and it’s a required parameter.
  • $name is name for the template, it is an optional argument. By default it’s null.
  • You can pass additional arguments using $args such as variables and values. By default an empty array will be passed.

What if the template file doesn’t exist?

Nothing will be imported in that case. There won’t be any errors either.

How to use get_template_part?

In get_template_part function, add the folder name and then the template page slug (without the .php extension).

You can have content.php or content-page.php. Both of them works!

Example

If you have this folder structure…


theme/
|-- partials/
|   |-- content.php
|-- page.php

…then you can import the content.php from partials like this:


get_template_part( 'partials/content', 'page' );


How to pass variables via get_template_part?


get_template_part(
    'partials/content',
    'page',
    array(
        'key'   => 'value',
        'title' => 'Title',
    )
);

How to get variables passed via get_template_part?

In your content.php file, you’d get the variables like this:


$key   = $args['key'];
$title = $args['title'];
//use them anywhere in your file now

If you’re not sure what variables are passed, just dump everything and go from there:


var_dump( $args );


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to import a CSS file using PHP code and not HTML code?PHPHow to overwrite file contents with new content in PHP?PHPWordPress plugin development: How to fix a SQL injection?WordPressHow to add and remove capabilities from a role in WordPress?WordPressResponsive Masonry Grid using CSS columns PropertyCSSDynamically generate names in SCSS (animation example)SCSS