get_block_templates()

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

get_block_templates( array$query=array(), string$template_type=’gc_template’)

Retrieves a list of unified template objects based on a query.

参数

$query

(array) (Optional) Arguments to retrieve templates.

  • ‘slug__in’
    (array) List of slugs to include.
  • ‘gc_id’
    (int) Post ID of customized template.

Default value: array()

$template_type

(string) (Optional) The template type (post type).

Default value: ‘gc_template’

响应

(GC_Block_Template[]) Block template objects.

源文件

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

function get_block_templates( $query = array(), $template_type = 'gc_template' ) {
	$gc_query_args = array(
		'post_status'    => array( 'auto-draft', 'draft', 'publish' ),
		'post_type'      => $template_type,
		'posts_per_page' => -1,
		'no_found_rows'  => true,
		'tax_query'      => array(
			array(
				'taxonomy' => 'gc_theme',
				'field'    => 'name',
				'terms'    => gc_get_theme()->get_stylesheet(),
			),
		),
	);

	if ( isset( $query['slug__in'] ) ) {
		$gc_query_args['post_name__in'] = $query['slug__in'];
	}

	// This is only needed for the regular templates CPT listing and editor.
	if ( isset( $query['gc_id'] ) ) {
		$gc_query_args['p'] = $query['gc_id'];
	} else {
		$gc_query_args['post_status'] = 'publish';
	}

	$template_query = new GC_Query( $gc_query_args );
	$query_result   = array();
	foreach ( $template_query->posts as $post ) {
		$template = _build_template_result_from_post( $post );

		if ( ! is_gc_error( $template ) ) {
			$query_result[] = $template;
		}
	}

	return $query_result;
}