{"pageProps":{"title":"block_template_part()","titleType":"Function","type":"functions","content":"

block_template_part() WordPress Function

The block_template_part() function is a versatile way to include template parts in your WordPress content. It can be used to include a template part in your post content, in a sidebar, or in any other location on your site.\n\nThis function makes it easy to reuse common template parts, such as a header or footer, across your site. You can also use it to include different template parts based on the context in which it is being used. For example, you could use a different header template part on your home page than you do on your post pages.\n\nThis function can be used with any WordPress theme. All you need to do is create a template part file with the appropriate code for the location where you want to include it. Then, you can use the block_template_part() function to include the template part in your content.

block_template_part( string $part ) #

Print a template-part.


Parameters

$part

(string)(Required)The template-part to print. Use "header" or "footer".


Top ↑

Source

File: wp-includes/block-template-utils.php

function block_template_part( $part ) {\n\t$template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' );\n\tif ( ! $template_part || empty( $template_part->content ) ) {\n\t\treturn;\n\t}\n\techo do_blocks( $template_part->content );\n}\n

Expand full source codeCollapse full source codeView on TracView on GitHub


Top ↑

Related

Top ↑

Uses

Uses
UsesDescription
wp-includes/block-template-utils.php:get_block_template()

Retrieves a single unified template object using its id.

wp-includes/blocks.php:do_blocks()

Parses dynamic blocks out of post_content and re-renders them.

wp-includes/theme.php:get_stylesheet()

Retrieves name of the current stylesheet.


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.
","description":"Print a template-part.","sidebarFiles":{"Hook":[{"componentTitle":"default_template_types","componentLink":"/reference/hooks/default_template_types","private":"","deprecated":""},{"componentTitle":"default_wp_template_part_areas","componentLink":"/reference/hooks/default_wp_template_part_areas","private":"","deprecated":""},{"componentTitle":"get_block_file_template","componentLink":"/reference/hooks/get_block_file_template","private":"","deprecated":""},{"componentTitle":"get_block_template","componentLink":"/reference/hooks/get_block_template","private":"","deprecated":""},{"componentTitle":"get_block_templates","componentLink":"/reference/hooks/get_block_templates","private":"","deprecated":""},{"componentTitle":"pre_get_block_file_template","componentLink":"/reference/hooks/pre_get_block_file_template","private":"","deprecated":""},{"componentTitle":"pre_get_block_template","componentLink":"/reference/hooks/pre_get_block_template","private":"","deprecated":""},{"componentTitle":"pre_get_block_templates","componentLink":"/reference/hooks/pre_get_block_templates","private":"","deprecated":""}],"Function":[{"componentTitle":"block_footer_area()","componentLink":"/reference/functions/block_footer_area","private":"","deprecated":""},{"componentTitle":"block_header_area()","componentLink":"/reference/functions/block_header_area","private":"","deprecated":""},{"componentTitle":"block_template_part()","componentLink":"/reference/functions/block_template_part","private":"","deprecated":"","active":"active"},{"componentTitle":"get_allowed_block_template_part_areas()","componentLink":"/reference/functions/get_allowed_block_template_part_areas","private":"","deprecated":""},{"componentTitle":"get_block_file_template()","componentLink":"/reference/functions/get_block_file_template","private":"","deprecated":""},{"componentTitle":"get_block_template()","componentLink":"/reference/functions/get_block_template","private":"","deprecated":""},{"componentTitle":"get_block_templates()","componentLink":"/reference/functions/get_block_templates","private":"","deprecated":""},{"componentTitle":"get_block_theme_folders()","componentLink":"/reference/functions/get_block_theme_folders","private":"","deprecated":""},{"componentTitle":"get_default_block_template_types()","componentLink":"/reference/functions/get_default_block_template_types","private":"","deprecated":""},{"componentTitle":"wp_generate_block_templates_export_file()","componentLink":"/reference/functions/wp_generate_block_templates_export_file","private":"","deprecated":""},{"componentTitle":"wp_is_theme_directory_ignored()","componentLink":"/reference/functions/wp_is_theme_directory_ignored","private":"","deprecated":""},{"componentTitle":"_add_block_template_info()","componentLink":"/reference/functions/_add_block_template_info","private":"private","deprecated":""},{"componentTitle":"_add_block_template_part_area_info()","componentLink":"/reference/functions/_add_block_template_part_area_info","private":"private","deprecated":""},{"componentTitle":"_build_block_template_result_from_file()","componentLink":"/reference/functions/_build_block_template_result_from_file","private":"private","deprecated":""},{"componentTitle":"_build_block_template_result_from_post()","componentLink":"/reference/functions/_build_block_template_result_from_post","private":"private","deprecated":""},{"componentTitle":"_build_template_result_from_post()","componentLink":"/reference/functions/_build_template_result_from_post","private":"private","deprecated":""},{"componentTitle":"_filter_block_template_part_area()","componentLink":"/reference/functions/_filter_block_template_part_area","private":"private","deprecated":""},{"componentTitle":"_flatten_blocks()","componentLink":"/reference/functions/_flatten_blocks","private":"private","deprecated":""},{"componentTitle":"_get_block_template_file()","componentLink":"/reference/functions/_get_block_template_file","private":"private","deprecated":""},{"componentTitle":"_get_block_templates_files()","componentLink":"/reference/functions/_get_block_templates_files","private":"private","deprecated":""},{"componentTitle":"_get_block_templates_paths()","componentLink":"/reference/functions/_get_block_templates_paths","private":"private","deprecated":""},{"componentTitle":"_inject_theme_attribute_in_block_template_content()","componentLink":"/reference/functions/_inject_theme_attribute_in_block_template_content","private":"private","deprecated":""},{"componentTitle":"_remove_theme_attribute_in_block_template_content()","componentLink":"/reference/functions/_remove_theme_attribute_in_block_template_content","private":"private","deprecated":""}]}},"__N_SSG":true}