Name Update Time
Netflix September 14, 2024 10:30 am
Disney+ September 10, 2024 10:09 am
Max September 14, 2024 10:20 am
ChatGPT 4 September 14, 2024 2:26 pm
Spotify September 14, 2024 2:08 pm
Prime Video September 14, 2024 2:22 pm
Codecademy September 14, 2024 2:13 pm
Grammarly September 14, 2024 4:45 pm
Canva Pro September 12, 2024 2:24 pm
Udemy Premium Cookies September 2, 2024 2:53 pm

To call a PHP file in a WordPress theme, you can use the get_template_part() function.

The get_template_part() function is used to get the PHP file specified in the theme directory. It is used to load another template file in the WordPress theme, which allows you to better organize and reuse your theme files.

Get_template_part() function usage reference: Detailed usage of get_template_part function in WordPress

The get_template_part() function has two parameters:

$slug: used to specify the name of the template file to be loaded, which can contain subdirectories but does not need to contain the file extension. If you need to load a template file named header.php in the root directory of the theme, you can use $slug = ‘header’.
$name (optional): used to specify a more specific name to distinguish different template files. If there are other variations in header.php, such as header-home.php, header-blog.php, etc., you can use the $name parameter to load these variations. If you do not need to distinguish different template files, you can omit the $name parameter.
Let’s look at some examples:

Load the theme’s header.php file:

get_template_part( ‘header’ );
Load the theme’s header-home.php file:

get_template_part( ‘header’, ‘home’ );
Load the theme’s partials/sidebar.php file:

get_template_part( ‘partials/sidebar’ );
Load the theme’s content.php file and pass some variables:

$post_type = get_post_type(); get_template_part( ‘content’, $post_type );
It is important to note that the get_template_part() function automatically loads a specific template file related to the current page. For example, in a single post page, if the $slug and $name parameters are not specified, the single.php file is loaded by default. In a category page, the category.php file is loaded by default. Therefore, the get_template_part() function can help you easily write more flexible and reusable WordPress theme code.