onetone_breadcrumbs_get_parents

The Onetone onetone breadcrumbs get parents function.

Description

onetone_breadcrumbs_get_parents( (string) $post_id = '', (string) $path = '' ); 

Parameters (2)

0. $post_id — Optional. (string) => ''
The post id.
1. $path — Optional. (string) => ''
The path.

Usage

  1. if ( !function_exists( 'onetone_breadcrumbs_get_parents' ) ) { 
  2. require_once '/includes/class-breadcrumb.php'; 
  3.  
  4. // The post id. 
  5. $post_id = ''; 
  6.  
  7. // The path. 
  8. $path = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = onetone_breadcrumbs_get_parents($post_id, $path); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/class-breadcrumb.php  
  1. function onetone_breadcrumbs_get_parents( $post_id = '', $path = '' ) { 
  2.  
  3. /** Set up an empty trail array. */ 
  4. $trail = array(); 
  5.  
  6. /** If neither a post ID nor path set, return an empty array. */ 
  7. if ( empty( $post_id ) && empty( $path ) ) 
  8. return $trail; 
  9.  
  10. /** If the post ID is empty, use the path to get the ID. */ 
  11. if ( empty( $post_id ) ) { 
  12.  
  13. /** Get parent post by the path. */ 
  14. $parent_page = get_page_by_path( $path ); 
  15.  
  16.  
  17. if( empty( $parent_page ) ) 
  18. // search on page name (single word) 
  19. $parent_page = get_page_by_title ( $path ); 
  20.  
  21. if( empty( $parent_page ) ) 
  22. // search on page title (multiple words) 
  23. $parent_page = get_page_by_title ( str_replace( array('-', '_'), ' ', $path ) ); 
  24.  
  25. /** End Modification */ 
  26.  
  27. /** If a parent post is found, set the $post_id variable to it. */ 
  28. if ( !empty( $parent_page ) ) 
  29. $post_id = $parent_page->ID; 
  30.  
  31. /** If a post ID and path is set, search for a post by the given path. */ 
  32. if ( $post_id == 0 && !empty( $path ) ) { 
  33.  
  34. /** Separate post names into separate paths by '/'. */ 
  35. $path = trim( $path, '/' ); 
  36. preg_match_all( "/\/.*?\z/", $path, $matches ); 
  37.  
  38. /** If matches are found for the path. */ 
  39. if ( isset( $matches ) ) { 
  40.  
  41. /** Reverse the array of matches to search for posts in the proper order. */ 
  42. $matches = array_reverse( $matches ); 
  43.  
  44. /** Loop through each of the path matches. */ 
  45. foreach ( $matches as $match ) { 
  46.  
  47. /** If a match is found. */ 
  48. if ( isset( $match[0] ) ) { 
  49.  
  50. /** Get the parent post by the given path. */ 
  51. $path = str_replace( $match[0], '', $path ); 
  52. $parent_page = get_page_by_path( trim( $path, '/' ) ); 
  53.  
  54. /** If a parent post is found, set the $post_id and break out of the loop. */ 
  55. if ( !empty( $parent_page ) && $parent_page->ID > 0 ) { 
  56. $post_id = $parent_page->ID; 
  57. break; 
  58.  
  59. /** While there's a post ID, add the post link to the $parents array. */ 
  60. while ( $post_id ) { 
  61.  
  62. /** Get the post by ID. */ 
  63. $page = get_page( $post_id ); 
  64.  
  65. /** Add the formatted post link to the array of parents. */ 
  66. $parents[] = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . get_the_title( $post_id ) . '</a>'; 
  67.  
  68. /** Set the parent post's parent to the post ID. */ 
  69. $post_id = $page->post_parent; 
  70.  
  71. /** If we have parent posts, reverse the array to put them in the proper order for the trail. */ 
  72. if ( isset( $parents ) ) 
  73. $trail = array_reverse( $parents ); 
  74.  
  75. /** Return the trail of parent posts. */ 
  76. return $trail; 
  77.  
  78. } // End onetone_breadcrumbs_get_parents()