Yoast\WP\SEO\Generators\Schema

Breadcrumb::generate()publicYoast 1.0

Returns Schema breadcrumb data to allow recognition of page's position in the site hierarchy.

Метод класса: Breadcrumb{}

Хуков нет.

Возвращает

true|false|Массив. Array on success, false on failure.

Использование

$Breadcrumb = new Breadcrumb();
$Breadcrumb->generate();

Код Breadcrumb::generate() Yoast 22.4

public function generate() {
	$breadcrumbs   = $this->context->presentation->breadcrumbs;
	$list_elements = [];

	// In case of pagination, replace the last breadcrumb, because it only contains "Page [number]" and has no URL.
	if (
		(
			$this->helpers->current_page->is_paged()
			|| $this->context->indexable->number_of_pages > 1
		) && (
			// Do not replace the last breadcrumb on static post pages.
			! $this->helpers->current_page->is_static_posts_page()
			// Do not remove the last breadcrumb if only one exists (bugfix for custom paginated frontpages).
			&& \count( $breadcrumbs ) > 1
		)
	) {
		\array_pop( $breadcrumbs );
	}

	// Only output breadcrumbs that are not hidden.
	$breadcrumbs = \array_filter( $breadcrumbs, [ $this, 'not_hidden' ] );

	\reset( $breadcrumbs );

	/*
	 * Check whether at least one of the breadcrumbs is broken.
	 * If so, do not output anything.
	 */
	foreach ( $breadcrumbs as $breadcrumb ) {
		if ( $this->is_broken( $breadcrumb ) ) {
			return false;
		}
	}

	// Create the last breadcrumb.
	$last_breadcrumb = \array_pop( $breadcrumbs );
	$breadcrumbs[]   = $this->format_last_breadcrumb( $last_breadcrumb );

	// If this is a static front page, prevent nested pages from creating a trail.
	if ( $this->helpers->current_page->is_home_static_page() ) {

		// Check if we're dealing with a nested page.
		if ( \count( $breadcrumbs ) > 1 ) {

			// Store the breadcrumbs home variable before dropping the parent page from the Schema.
			$breadcrumbs_home = $breadcrumbs[0]['text'];
			$breadcrumbs      = [ \array_pop( $breadcrumbs ) ];

			// Make the child page show the breadcrumbs home variable rather than its own title.
			$breadcrumbs[0]['text'] = $breadcrumbs_home;
		}
	}

	$breadcrumbs = \array_filter( $breadcrumbs, [ $this, 'not_empty_text' ] );
	$breadcrumbs = \array_values( $breadcrumbs );

	// Create intermediate breadcrumbs.
	foreach ( $breadcrumbs as $index => $breadcrumb ) {
		$list_elements[] = $this->create_breadcrumb( $index, $breadcrumb );
	}

	return [
		'@type'           => 'BreadcrumbList',
		'@id'             => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH,
		'itemListElement' => $list_elements,
	];
}