summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/theme-tools/site-breadcrumbs.php')
-rw-r--r--plugins/jetpack/modules/theme-tools/site-breadcrumbs.php70
1 files changed, 56 insertions, 14 deletions
diff --git a/plugins/jetpack/modules/theme-tools/site-breadcrumbs.php b/plugins/jetpack/modules/theme-tools/site-breadcrumbs.php
index 3b783eea..7919d5df 100644
--- a/plugins/jetpack/modules/theme-tools/site-breadcrumbs.php
+++ b/plugins/jetpack/modules/theme-tools/site-breadcrumbs.php
@@ -2,7 +2,7 @@
/**
* Plugin Name: Site Breadcrumbs
* Plugin URI: http://wordpress.com
- * Description: Quickly add breadcrumbs to the single view of a hierarchical post type
+ * Description: Quickly add breadcrumbs to the single view of a hierarchical post type or a hierarchical taxonomy.
* Author: Automattic
* Version: 1.0
* Author URI: http://wordpress.com
@@ -10,28 +10,70 @@
*/
function jetpack_breadcrumbs() {
- if ( ! is_page() || is_front_page() ) {
+ $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;
}
- global $post;
+ $breadcrumb = '';
- $ancestors = array_reverse( get_post_ancestors( $post->ID ) );
+ 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="http://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="http://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 );
- $before = '<nav class="entry-breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">';
- $after = '</nav>';
+ if ( is_wp_error( $current ) ) {
+ return;
+ }
- $home = '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( home_url( "/" ) ) . '" class="home-link" itemprop="item" rel="home"><span itemprop="name">' . __( 'Home', 'jetpack' ) . '</span></a></span>';
+ if ( $current->parent ) {
+ $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy );
+ }
- $breadcrumb = '';
+ $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>';
+ }
- if ( $ancestors ) {
- foreach ( $ancestors as $ancestor ) {
- $breadcrumb .= '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a href="' . esc_url( get_permalink( $ancestor ) ) . '" itemprop="item"><span itemprop="name">' . esc_html( get_the_title( $ancestor ) ) . '</span></a></span>';
- }
+ $home = '<span itemprop="itemListElement" itemscope itemtype="http://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="http://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 );
}
- $breadcrumb .= '<span class="current-page" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">' . esc_html( get_the_title( $post->ID ) ) . '</span></span>';
+ $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
- echo $before . $home . $breadcrumb . $after;
+ return $chain;
}