compat
3 years ago
content-options
2 years ago
js
6 years ago
responsive-videos
3 years ago
site-logo
3 years ago
social-menu
2 years ago
content-options.php
4 years ago
devicepx.php
5 years ago
featured-content.php
3 years ago
infinite-scroll.php
4 years ago
random-redirect.php
3 years ago
responsive-videos.php
3 years ago
site-breadcrumbs.php
3 years ago
site-logo.php
4 years ago
social-links.php
3 years ago
social-menu.php
4 years ago
site-breadcrumbs.php
90 lines
| 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 | * Text Domain: jetpack |
| 11 | * |
| 12 | * @package automattic/jetpack |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Echos a set of breadcrumbs. |
| 17 | * |
| 18 | * Themes can call this function where the breadcrumbs should be outputted. |
| 19 | */ |
| 20 | function jetpack_breadcrumbs() { |
| 21 | $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' ); |
| 22 | $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy ); |
| 23 | |
| 24 | $post_type = is_page() ? 'page' : get_query_var( 'post_type' ); |
| 25 | $is_post_type_hierarchical = is_post_type_hierarchical( $post_type ); |
| 26 | |
| 27 | if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $breadcrumb = ''; |
| 32 | $position = 1; |
| 33 | |
| 34 | if ( $is_post_type_hierarchical ) { |
| 35 | $post_id = get_queried_object_id(); |
| 36 | $ancestors = array_reverse( get_post_ancestors( $post_id ) ); |
| 37 | if ( $ancestors ) { |
| 38 | foreach ( $ancestors as $ancestor ) { |
| 39 | $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>'; |
| 40 | ++$position; |
| 41 | } |
| 42 | } |
| 43 | $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( get_the_title( $post_id ) ) . '</span></span>'; |
| 44 | } elseif ( $is_taxonomy_hierarchical ) { |
| 45 | $current = get_term( get_queried_object_id(), $taxonomy ); |
| 46 | |
| 47 | if ( is_wp_error( $current ) ) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | if ( $current->parent ) { |
| 52 | $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy ); |
| 53 | } |
| 54 | |
| 55 | $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta property="position" content="' . esc_attr( $position ) . '"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>'; |
| 56 | } |
| 57 | |
| 58 | $home = '<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><meta itemprop="position" content="' . esc_attr( $position ) . '"><meta itemprop="position" content="0"><a href="' . esc_url( home_url( '/' ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . esc_html__( 'Home', 'jetpack' ) . '</span></a></span>'; |
| 59 | |
| 60 | echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Return the parents for a given taxonomy term ID. |
| 65 | * |
| 66 | * @param int $term Taxonomy term whose parents will be returned. |
| 67 | * @param string $taxonomy Taxonomy name that the term belongs to. |
| 68 | * @param array $visited Terms already added to prevent duplicates. |
| 69 | * |
| 70 | * @return string A list of links to the term parents. |
| 71 | */ |
| 72 | function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) { |
| 73 | $parent = get_term( $term, $taxonomy ); |
| 74 | |
| 75 | if ( is_wp_error( $parent ) ) { |
| 76 | return $parent; |
| 77 | } |
| 78 | |
| 79 | $chain = ''; |
| 80 | |
| 81 | if ( $parent->parent && ( $parent->parent !== $parent->term_id ) && ! in_array( $parent->parent, $visited, true ) ) { |
| 82 | $visited[] = $parent->parent; |
| 83 | $chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited ); |
| 84 | } |
| 85 | |
| 86 | $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>'; |
| 87 | |
| 88 | return $chain; |
| 89 | } |
| 90 |