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