blob: 2f2667387696696a5de3adbdd72d5bcb3f32fe48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?php
/**
* Plugin Name: Site Breadcrumbs
* Plugin URI: https://wordpress.com
* Description: Quickly add breadcrumbs to the single view of a hierarchical post type or a hierarchical taxonomy.
* Author: Automattic
* Version: 1.0
* Author URI: https://wordpress.com
* License: GPL2 or later
*/
function jetpack_breadcrumbs() {
$taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' );
$is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy );
$post_type = is_page() ? 'page' : get_query_var( 'post_type' );
$is_post_type_hierarchical = is_post_type_hierarchical( $post_type );
if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) {
return;
}
$breadcrumb = '';
if ( $is_post_type_hierarchical ) {
$post_id = get_queried_object_id();
$ancestors = array_reverse( get_post_ancestors( $post_id ) );
if ( $ancestors ) {
foreach ( $ancestors as $ancestor ) {
$breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>';
}
}
$breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>';
} elseif ( $is_taxonomy_hierarchical ) {
$current = get_term( get_queried_object_id(), $taxonomy );
if ( is_wp_error( $current ) ) {
return;
}
if ( $current->parent ) {
$breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy );
}
$breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>';
}
$home = '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>';
echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>';
}
/**
* Return the parents for a given taxonomy term ID.
*
* @param int $term Taxonomy term whose parents will be returned.
* @param string $taxonomy Taxonomy name that the term belongs to.
* @param array $visited Terms already added to prevent duplicates.
*
* @return string A list of links to the term parents.
*/
function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) {
$parent = get_term( $term, $taxonomy );
if ( is_wp_error( $parent ) ) {
return $parent;
}
$chain = '';
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited );
}
$chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
return $chain;
}
|