The locate_block_template() WordPress PHP function finds a block template with equal or higher specificity than a given PHP template file.
Usage
locate_block_template( $template, $type, $templates );
Parameters
$template (string)
– Path to the template. Seelocate_template()
.$type (string)
– Sanitized filename without extension.$templates (array)
– A list of template candidates, in descending order of priority.
More information
See WordPress Developer Resources: locate_block_template()
Examples
Find a block template for a custom post type
This code snippet will search for a block template for the custom post type portfolio
.
$template = 'single-portfolio'; $type = 'portfolio'; $templates = array( 'block-portfolio.html', 'block-single-portfolio.html', 'block-single.html', ); $block_template = locate_block_template( $template, $type, $templates );
Locate a block template for a category
This code snippet searches for a block template for the category with ID 3
.
$template = 'category-3'; $type = 'category'; $templates = array( 'block-category-3.html', 'block-category.html', ); $block_template = locate_block_template( $template, $type, $templates );
Find a block template for a specific page
This code snippet will search for a block template for a specific page with slug about-us
.
$template = 'page-about-us'; $type = 'page'; $templates = array( 'block-page-about-us.html', 'block-page.html', ); $block_template = locate_block_template( $template, $type, $templates );
Locate a block template for an author
This code snippet searches for a block template for an author with the username johndoe
.
$template = 'author-johndoe'; $type = 'author'; $templates = array( 'block-author-johndoe.html', 'block-author.html', ); $block_template = locate_block_template( $template, $type, $templates );
Find a block template for a custom taxonomy
This code snippet will search for a block template for a custom taxonomy named genre
.
$template = 'taxonomy-genre'; $type = 'taxonomy'; $templates = array( 'block-taxonomy-genre.html', 'block-taxonomy.html', ); $block_template = locate_block_template( $template, $type, $templates );