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.
Search
Popular on Blogar
Content Marketing-What is Content Marketing
- October 24th, 2023
- 1401 Views
Social Media Marketing-Social Media Marketing (SMM)
- October 24th, 2023
- 1692 Views
Pay-Per-Click-What is PPC
- October 24th, 2023
- 1271 Views
How to Activate Windows 11 Pro (No tools needed)
- September 3rd, 2024
- 99 Views
How to disable automatic updates for themes and plugins in WordPress
- August 16th, 2024
- 97 Views
Get all WordPress category names and IDs
- August 21st, 2024
- 95 Views
WordPress sticky articles in categories but not on the homepage
- August 13th, 2024
- 94 Views
WordPress-How to show single post content in front-end?
- August 26th, 2024
- 91 Views
WordPress display articles by category
- August 28th, 2024
- 86 Views
How To Find The IP Of Any Whatsapp Call
- September 3rd, 2024
- 85 Views
Recent Post
Content Marketing-What is Content Marketing
- October 24th, 2023
- 1401 Views
Social Media Marketing-Social Media Marketing (SMM)
- October 24th, 2023
- 1692 Views
Pay-Per-Click-What is PPC
- October 24th, 2023
- 1271 Views
VMware cannot install VMware Tools, prompts VMCI, memory driver
- November 25th, 2024
- 57 Views
More Post
- Howo Tractor Truck
- Howo Tipper/Dump Truck
- Howo Cargo Truck
- Howo Concrete Mixer Truck
- Howo Special Truck
- Howo Light Truck
- Shacman Tractor Truck
- Shacman Tipper/Dump Truck
- Shacman Cargo Truck
- Shacman Concrete Mixer Truck
- LGMG Mining Trucks
- XCMG Wheel Loader
- XCMG Excavator
- XCMG Crane
- XCMG Asphalt Pavers
- XCMG Road Roller
- Shantui Bulldozer
Copyright © 2024 BBBBF All Rights Reserved.