site-breadcrumbs.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. function jetpack_breadcrumbs() {
  12. $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' );
  13. $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy );
  14. $post_type = is_page() ? 'page' : get_query_var( 'post_type' );
  15. $is_post_type_hierarchical = is_post_type_hierarchical( $post_type );
  16. if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) {
  17. return;
  18. }
  19. $breadcrumb = '';
  20. if ( $is_post_type_hierarchical ) {
  21. $post_id = get_queried_object_id();
  22. $ancestors = array_reverse( get_post_ancestors( $post_id ) );
  23. if ( $ancestors ) {
  24. foreach ( $ancestors as $ancestor ) {
  25. $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>';
  26. }
  27. }
  28. $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>';
  29. } elseif ( $is_taxonomy_hierarchical ) {
  30. $current = get_term( get_queried_object_id(), $taxonomy );
  31. if ( is_wp_error( $current ) ) {
  32. return;
  33. }
  34. if ( $current->parent ) {
  35. $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy );
  36. }
  37. $breadcrumb .= '<span class="current-category" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><span itemprop="name">' . esc_html( $current->name ) . '</span></span>';
  38. }
  39. $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>';
  40. echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>';
  41. }
  42. /**
  43. * Return the parents for a given taxonomy term ID.
  44. *
  45. * @param int $term Taxonomy term whose parents will be returned.
  46. * @param string $taxonomy Taxonomy name that the term belongs to.
  47. * @param array $visited Terms already added to prevent duplicates.
  48. *
  49. * @return string A list of links to the term parents.
  50. */
  51. function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) {
  52. $parent = get_term( $term, $taxonomy );
  53. if ( is_wp_error( $parent ) ) {
  54. return $parent;
  55. }
  56. $chain = '';
  57. if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) ) {
  58. $visited[] = $parent->parent;
  59. $chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited );
  60. }
  61. $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
  62. return $chain;
  63. }