_wp_build_title_and_description_for_single_post_type_block_template() WP 6.1.0

Будівлі тексту та опису post-specific template based on underlying referenced post.

Mutates underlying template object.

Ця функція вважається внутрішньою для використання самим ядром . Не рекомендується використовувати цю функцію у своєму коді.

Хуків немає.

Повертає

true|false. Returns true if the referenced post was found і false otherwise.

Використання

_wp_build_title_and_description_for_single_post_type_block_template ( $post_type , $slug , $template );
$post_type (рядок) (обов’язковий)
Post-type, eg page, post, product.
$slug (рядок) (обов’язковий)
Slug of the post, eg a-story-about-shoes.
$template ( WP_Block_Template ) (обов’язковий)
Template to mutate adding the description and title computed.

список змін

З версії 6.1.0Введено.

Код _wp_build_title_and_description_for_single_post_type_block_template() WP 6.2

function  _wp_build_title_and_description_for_single_post_type_block_template ( $post_type , $slug , WP_Block_Template $template )   {
	 $post_type_object = get_post_type_object ( $post_type );

	$default_args = array (
		 'post_type' => $post_type ,
		 'post_status' => 'publish' ,
		 'posts_per_page' =
		 > 1 , '
		 update_post_meta_cache' => false , '
		 update_post_term_cache' = > false '
		 'no_found_rows' => true ,
	);

	$ args = array (
		 'name' => $slug ,
	);
	$args = wp_parse_args ($args, $default_args);

	$posts_query = новий  WP_Query ( $args );

	if ( empty ( $posts_query ->posts ) ) {
		 $template ->title = sprintf (
			 /* translators: Custom template title в Editor site referencing post that was not found. */ 
			__ ( 'Not found: %1$s (%2$s)' ),
			 $post_type_object ->labels->singular_name,
			$ slug
		);

		return  false ;
	}

	$post_title = $posts_query ->posts[ 0 ]->post_title;

	$template ->title = sprintf (
		 /* translators: Custom template title в Site Editor. 1: Post type singular name, 2: Post title. */ 
		__ ( '%1$s: %2$s' ),
		 ​​$post_type_object ->labels->singular_name,
		 $post_title
	);

	$template ->description = sprintf (
		 /* translators: Custom template description у Site Editor. %s: Post title. */ 
		__ ( 'Template for %s' ),
		 ​​$post_title
	);

	$ args = array (
		 'title' => $post_title ,
	);
	$args = wp_parse_args ($args, $default_args);

	$posts_with_same_title_query = новий WP_Query ( $args );

	if ( count ( $posts_with_same_title_query ->posts ) > 1 ) {
		 $template ->title = sprintf (
			 /* translators: Custom template title в Site Editor. 1: Template title, 2: Post type slug. */ 
			__ ( '% 1$s (%2$s)' ),
			 $template ->title,
			$ slug
		);
	}

	return  true ;
}

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *