get_block_template()

最后更新于:2021-11-26 05:46:20

get_block_template( string$id, string$template_type=’gc_template’)

Retrieves a single unified template object using its id.

参数

$id

(string) (Required) Template unique identifier (example: theme_slug//template_slug).

$template_type

(string) (Optional) gc_template.

Default value: ‘gc_template’

响应

(GC_Block_Template|null) Template.

源文件

文件: gc-includes/block-template-utils.php

function get_block_template( $id, $template_type = 'gc_template' ) {
	$parts = explode( '//', $id, 2 );
	if ( count( $parts ) < 2 ) {
		return null;
	}
	list( $theme, $slug ) = $parts;
	$gc_query_args        = array(
		'post_name__in'  => array( $slug ),
		'post_type'      => $template_type,
		'post_status'    => array( 'auto-draft', 'draft', 'publish', 'trash' ),
		'posts_per_page' => 1,
		'no_found_rows'  => true,
		'tax_query'      => array(
			array(
				'taxonomy' => 'gc_theme',
				'field'    => 'name',
				'terms'    => $theme,
			),
		),
	);
	$template_query       = new GC_Query( $gc_query_args );
	$posts                = $template_query->posts;

	if ( count( $posts ) > 0 ) {
		$template = _build_template_result_from_post( $posts[0] );

		if ( ! is_gc_error( $template ) ) {
			return $template;
		}
	}

	return null;
}