wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren
block_core_breadcrumbs_get_terms_breadcrumbs › WordPress Function
Seit7.0.0
Veraltetn/v
› block_core_breadcrumbs_get_terms_breadcrumbs ( $post_id, $post_type )
| Parameter: (2) |
|
| Gibt zurück: |
|
| Definiert in: |
|
| Codex: |
Generates breadcrumb items from taxonomy terms.
Finds the first publicly queryable taxonomy with terms assigned to the post and generates breadcrumb links, including hierarchical term ancestors if applicable.Quellcode
function block_core_breadcrumbs_get_terms_breadcrumbs( $post_id, $post_type ) {
$breadcrumb_items = array();
// Get public taxonomies for this post type.
$taxonomies = wp_filter_object_list(
get_object_taxonomies( $post_type, 'objects' ),
array(
'publicly_queryable' => true,
'show_in_rest' => true,
)
);
if ( empty( $taxonomies ) ) {
return $breadcrumb_items;
}
/**
* Filters breadcrumb settings (taxonomy and term selection) for a post or post type.
*
* Allows developers to specify which taxonomy and term should be used in the
* breadcrumb trail when a post type has multiple taxonomies or when a post is
* assigned to multiple terms within a taxonomy.
*
* @since 7.0.0
*
* @param array $settings {
* Array of breadcrumb settings. Default empty array.
*
* @type string $taxonomy Optional. Taxonomy slug to use for breadcrumbs.
* The taxonomy must be registered for the post type and have
* terms assigned to the post. If not found or has no terms,
* fall back to the first available taxonomy with terms.
* @type string $term Optional. Term slug to use when the post has multiple terms
* in the selected taxonomy. If the term is not found or not
* assigned to the post, fall back to the first term. If the
* post has only one term, that term is used regardless.
* }
* @param string $post_type The post type slug.
* @param int $post_id The post ID.
*/
$settings = apply_filters( 'block_core_breadcrumbs_post_type_settings', array(), $post_type, $post_id );
$taxonomy_name = null;
$terms = array();
// Try preferred taxonomy first if specified.
if ( ! empty( $settings['taxonomy'] ) ) {
foreach ( $taxonomies as $taxonomy ) {
if ( $taxonomy->name === $settings['taxonomy'] ) {
$post_terms = get_the_terms( $post_id, $taxonomy->name );
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
$taxonomy_name = $taxonomy->name;
$terms = $post_terms;
}
break;
}
}
}
// If no preferred taxonomy or it didn't have terms, find the first taxonomy with terms.
if ( empty( $terms ) ) {
foreach ( $taxonomies as $taxonomy ) {
$post_terms = get_the_terms( $post_id, $taxonomy->name );
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
$taxonomy_name = $taxonomy->name;
$terms = $post_terms;
break;
}
}
}
if ( ! empty( $terms ) ) {
// Select which term to use.
$term = reset( $terms );
// Try preferred term if specified and post has multiple terms.
if ( ! empty( $settings['term'] ) && count( $terms ) > 1 ) {
foreach ( $terms as $candidate_term ) {
if ( $candidate_term->slug === $settings['term'] ) {
$term = $candidate_term;
break;
}
}
}
// Add hierarchical term ancestors if applicable.
$breadcrumb_items = array_merge(
$breadcrumb_items,
block_core_breadcrumbs_get_term_ancestors_items( $term->term_id, $taxonomy_name )
);
$breadcrumb_items[] = array(
'label' => $term->name,
'url' => get_term_link( $term ),
);
}
return $breadcrumb_items;
}