wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



get_block_template › WordPress Function

Seit5.8.0
Veraltetn/v
get_block_template ( $id, $template_type = 'wp_template' )
Parameter: (2)
  • (string) $id Template unique identifier (example: theme_slug//template_slug).
    Erforderlich: Ja
  • (string) $template_type Optional. The template type (post type). Default 'wp_template'.
    Erforderlich: Nein
    Standard: 'wp_template'
Gibt zurück:
  • (WP_Block_Template|null) Template.
Definiert in:
Codex:

Retrieves a single unified template object using its id.



Quellcode

function get_block_template( $id, $template_type = 'wp_template' ) {
	$parts = explode( '//', $id, 2 );
	if ( count( $parts ) < 2 ) {
		return null;
	}
	list( $theme, $slug ) = $parts;
	$wp_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' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme,
			),
		),
	);
	$template_query       = new WP_Query( $wp_query_args );
	$posts                = $template_query->posts;

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

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

	return null;
}