Brak opisu

site-breadcrumbs.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. * Echos a set of breadcrumbs.
  16. *
  17. * Themes can call this function where the breadcrumbs should be outputted.
  18. */
  19. function jetpack_breadcrumbs() {
  20. $taxonomy = is_category() ? 'category' : get_query_var( 'taxonomy' );
  21. $is_taxonomy_hierarchical = is_taxonomy_hierarchical( $taxonomy );
  22. $post_type = is_page() ? 'page' : get_query_var( 'post_type' );
  23. $is_post_type_hierarchical = is_post_type_hierarchical( $post_type );
  24. if ( ! ( $is_post_type_hierarchical || $is_taxonomy_hierarchical ) || is_front_page() ) {
  25. return;
  26. }
  27. $breadcrumb = '';
  28. $position = 1;
  29. if ( $is_post_type_hierarchical ) {
  30. $post_id = get_queried_object_id();
  31. $ancestors = array_reverse( get_post_ancestors( $post_id ) );
  32. if ( $ancestors ) {
  33. foreach ( $ancestors as $ancestor ) {
  34. $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>';
  35. $position++;
  36. }
  37. }
  38. $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>';
  39. } elseif ( $is_taxonomy_hierarchical ) {
  40. $current = get_term( get_queried_object_id(), $taxonomy );
  41. if ( is_wp_error( $current ) ) {
  42. return;
  43. }
  44. if ( $current->parent ) {
  45. $breadcrumb = jetpack_get_term_parents( $current->parent, $taxonomy );
  46. }
  47. $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>';
  48. }
  49. $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>';
  50. echo '<nav class="entry-breadcrumbs" itemscope itemtype="https://schema.org/BreadcrumbList">' . $home . $breadcrumb . '</nav>'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  51. }
  52. /**
  53. * Return the parents for a given taxonomy term ID.
  54. *
  55. * @param int $term Taxonomy term whose parents will be returned.
  56. * @param string $taxonomy Taxonomy name that the term belongs to.
  57. * @param array $visited Terms already added to prevent duplicates.
  58. *
  59. * @return string A list of links to the term parents.
  60. */
  61. function jetpack_get_term_parents( $term, $taxonomy, $visited = array() ) {
  62. $parent = get_term( $term, $taxonomy );
  63. if ( is_wp_error( $parent ) ) {
  64. return $parent;
  65. }
  66. $chain = '';
  67. if ( $parent->parent && ( $parent->parent !== $parent->term_id ) && ! in_array( $parent->parent, $visited, true ) ) {
  68. $visited[] = $parent->parent;
  69. $chain .= jetpack_get_term_parents( $parent->parent, $taxonomy, $visited );
  70. }
  71. $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $parent->name . '</a>';
  72. return $chain;
  73. }