render_block_core_query_no_results

函式
render_block_core_query_no_results ( $attributes, $content, $block )
引數
  • (array) $attributes Block attributes.
    Required:
  • (string) $content Block default content.
    Required:
  • (WP_Block) $block Block instance.
    Required:
返回值
  • (string) Returns the wrapper for the no results block.
定義位置
相關方法
register_block_core_query_no_resultsrender_block_core_query_pagination_previousrender_block_core_query_titlerender_block_core_query_paginationrender_block_core_rss
引入
-
棄用
-

render_block_core_query_no_results: 這個函式用於在查詢沒有返回結果時,渲染一個塊的HTML內容。它與WordPress中的查詢塊一起使用,允許使用者根據某些標準來顯示文章、頁面或自定義文章型別。如果查詢沒有返回結果,該塊將向使用者顯示一條沒有匹配結果的資訊: 這個函式負責渲染該資訊。

渲染伺服器上的`core/query-no-results`區塊。

function render_block_core_query_no_results( $attributes, $content, $block ) {
	if ( empty( trim( $content ) ) ) {
		return '';
	}

	$page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
	$page     = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];

	// Override the custom query with the global query if needed.
	$use_global_query = ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] );
	if ( $use_global_query ) {
		global $wp_query;
		$query = $wp_query;
	} else {
		$query_args = build_query_vars_from_query_block( $block, $page );
		$query      = new WP_Query( $query_args );
	}

	if ( $query->have_posts() ) {
		return '';
	}

	if ( ! $use_global_query ) {
		wp_reset_postdata();
	}

	return sprintf(
		'<div %1$s>%2$s</div>',
		get_block_wrapper_attributes(),
		$content
	);
}

常見問題

FAQs
檢視更多 >