Advertisement
Guest User

class_breadcrumbs

a guest
Jan 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.54 KB | None | 0 0
  1. <?php
  2.  
  3. if(!class_exists('avia_breadcrumb'))
  4. {
  5.     class avia_breadcrumb
  6.     {
  7.         var $options;
  8.  
  9.     function __construct($options = ""){
  10.  
  11.         $this->options = array(     //change this array if you want another output scheme
  12.         'before' => '<span class="arrow"> ',
  13.         'after' => ' </span>',
  14.         'delimiter' => '&raquo;'
  15.         );
  16.  
  17.         if(is_array($options))
  18.         {
  19.             $this->options = array_merge($this->options, $options);
  20.         }
  21.  
  22.  
  23.         $markup = $this->options['before'].$this->options['delimiter'].$this->options['after'];
  24.  
  25.         global $post;
  26.         echo '<p class="breadcrumb"><span class="breadcrumb_info">'.__('You are here:','avia_framework').'</span> <a href="'.get_bloginfo('url').'">';
  27.             bloginfo('name');
  28.             echo "</a>";
  29.             if(!is_front_page()){echo $markup;}
  30.  
  31.             $output = $this->simple_breadcrumb_case($post);
  32.  
  33.             echo "<span class='current_crumb'>";
  34.             if ( is_page() || is_single()) {
  35.             the_title();
  36.             }else{
  37.             echo $output;
  38.             }
  39.             echo " </span></p>";
  40.         }
  41.  
  42.         function simple_breadcrumb_case($der_post)
  43.         {
  44.             global $post;
  45.  
  46.             $markup = $this->options['before'].$this->options['delimiter'].$this->options['after'];
  47.             if (is_page()){
  48.                  if($der_post->post_parent) {
  49.                      $my_query = get_post($der_post->post_parent);
  50.                      $this->simple_breadcrumb_case($my_query);
  51.                      $link = '<a href="';
  52.                      $link .= get_permalink($my_query->ID);
  53.                      $link .= '">';
  54.                      $link .= ''. get_the_title($my_query->ID) . '</a>'. $markup;
  55.                      echo $link;
  56.                   }
  57.             return;
  58.             }
  59.  
  60.         if(is_single())
  61.         {
  62.             $category = get_the_category();
  63.             if (is_attachment()){
  64.  
  65.                 $my_query = get_post($der_post->post_parent);
  66.                 $category = get_the_category($my_query->ID);
  67.  
  68.                 if(isset($category[0]))
  69.                 {
  70.                     $ID = $category[0]->cat_ID;
  71.                     $parents = get_category_parents($ID, TRUE, $markup, FALSE );
  72.                     if(!is_object($parents)) echo $parents;
  73.                     previous_post_link("%link $markup");
  74.                 }
  75.  
  76.             }else{
  77.  
  78.                 $postType = get_post_type();
  79.  
  80.                 if($postType == 'post')
  81.                 {
  82.                     $ID = $category[0]->cat_ID;
  83.                     echo get_category_parents($ID, TRUE, $markup, FALSE );
  84.                 }
  85.                 else if($postType == 'portfolio')
  86.                 {
  87.                     $terms = get_the_term_list( $post->ID, 'portfolio_entries', '', '$$$', '' );
  88.                     $terms = explode('$$$',$terms);
  89.                     echo $terms[0].$markup;
  90.  
  91.                 }
  92.             }
  93.         return;
  94.         }
  95.  
  96.         if(is_tax()){
  97.               $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
  98.               return $term->name;
  99.  
  100.         }
  101.  
  102.  
  103.         if(is_category()){
  104.             $category = get_the_category();
  105.             $i = $category[0]->cat_ID;
  106.             $parent = $category[0]-> category_parent;
  107.  
  108.             if($parent > 0 && $category[0]->cat_name == single_cat_title("", false)){
  109.                 echo get_category_parents($parent, TRUE, $markup, FALSE);
  110.             }
  111.         return single_cat_title('',FALSE);
  112.         }
  113.  
  114.  
  115.         if(is_author()){
  116.             $curauth = get_userdatabylogin(get_query_var('author_name'));
  117.             return "Author: ".$curauth->nickname;
  118.         }
  119.         if(is_tag()){ return "Tag: ".single_tag_title('',FALSE); }
  120.  
  121.         if(is_404()){ return __("404 - Page not Found",'avia_framework'); }
  122.  
  123.         if(is_search()){ return __("Search",'avia_framework');}
  124.  
  125.         if(is_year()){ return get_the_time('Y'); }
  126.  
  127.         if(is_month()){
  128.         $k_year = get_the_time('Y');
  129.         echo "<a href='".get_year_link($k_year)."'>".$k_year."</a>".$markup;
  130.         return get_the_time('F'); }
  131.  
  132.         if(is_day() || is_time()){
  133.         $k_year = get_the_time('Y');
  134.         $k_month = get_the_time('m');
  135.         $k_month_display = get_the_time('F');
  136.         echo "<a href='".get_year_link($k_year)."'>".$k_year."</a>".$markup;
  137.         echo "<a href='".get_month_link($k_year, $k_month)."'>".$k_month_display."</a>".$markup;
  138.  
  139.         return get_the_time('jS (l)'); }
  140.  
  141.         }
  142.  
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /*-----------------------------------------------------------------------------------*/
  150. /* avia_breadcrumbs() - Custom breadcrumb generator function  */
  151. /*
  152. /* Params:
  153. /*
  154. /* Arguments Array:
  155. /*
  156. /* 'separator'          - The character to display between the breadcrumbs.
  157. /* 'before'             - HTML to display before the breadcrumbs.
  158. /* 'after'              - HTML to display after the breadcrumbs.
  159. /* 'front_page'         - Include the front page at the beginning of the breadcrumbs.
  160. /* 'show_home'          - If $show_home is set and we're not on the front page of the site, link to the home page.
  161. /* 'echo'               - Specify whether or not to echo the breadcrumbs. Alternative is "return".
  162. /* 'show_posts_page'    - If a static front page is set and there is a posts page, toggle whether or not to display that page's tree.
  163. /*
  164. /*-----------------------------------------------------------------------------------*/
  165.  
  166.  
  167.  
  168. /**
  169.  * The code below is an inspired/modified version by woothemes breadcrumb function which in turn is inspired by Justin Tadlock's Hybrid Core :)
  170.  */
  171.  
  172.  
  173. function avia_breadcrumbs( $args = array() ) {
  174.     global $wp_query, $wp_rewrite;
  175.  
  176.     /* Create an empty variable for the breadcrumb. */
  177.     $breadcrumb = '';
  178.  
  179.     /* Create an empty array for the trail. */
  180.     $trail = array();
  181.     $path = '';
  182.  
  183.     /* Set up the default arguments for the breadcrumb. */
  184.     $defaults = array(
  185.         'separator' => '&raquo;',
  186.         'before' => '<span class="breadcrumb-title">' . __( 'You are here:', 'avia_framework' ) . '</span>',
  187.         'after' => false,
  188.         'front_page' => true,
  189.         'show_home' => __( 'Home', 'avia_framework' ),
  190.         'echo' => false,
  191.         'show_categories' => true,
  192.         'show_posts_page' => true,
  193.         'truncate' => 30,
  194.         'richsnippet' => false
  195.     );
  196.  
  197.  
  198.     /* Allow singular post views to have a taxonomy's terms prefixing the trail. */
  199.     if ( is_singular() )
  200.         $defaults["singular_{$wp_query->post->post_type}_taxonomy"] = false;
  201.  
  202.     /* Apply filters to the arguments. */
  203.     $args = apply_filters( 'avia_breadcrumbs_args', $args );
  204.  
  205.     /* Parse the arguments and extract them for easy variable naming. */
  206.     extract( wp_parse_args( $args, $defaults ) );
  207.  
  208.     /* If $show_home is set and we're not on the front page of the site, link to the home page. */
  209.     if ( !is_front_page() && $show_home )
  210.         $trail[] = '<a href="' . home_url() . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . '" rel="home" class="trail-begin">' . $show_home . '</a>';
  211.  
  212.     /* If viewing the front page of the site. */
  213.     if ( is_front_page() ) {
  214.         if ( !$front_page )
  215.             $trail = false;
  216.         elseif ( $show_home )
  217.             $trail['trail_end'] = "{$show_home}";
  218.     }
  219.  
  220.     /* If viewing the "home"/posts page. */
  221.     elseif ( is_home() ) {
  222.         $home_page = get_page( $wp_query->get_queried_object_id() );
  223.         $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $home_page->post_parent, '' ) );
  224.         $trail['trail_end'] = get_the_title( $home_page->ID );
  225.     }
  226.  
  227.     /* If viewing a singular post (page, attachment, etc.). */
  228.     elseif ( is_singular() ) {
  229.  
  230.         /* Get singular post variables needed. */
  231.         $post = $wp_query->get_queried_object();
  232.         $post_id = absint( $wp_query->get_queried_object_id() );
  233.         $post_type = $post->post_type;
  234.         $parent = $post->post_parent;
  235.  
  236.  
  237.         /* If a custom post type, check if there are any pages in its hierarchy based on the slug. */
  238.         if ( 'page' !== $post_type && 'post' !== $post_type ) {
  239.  
  240.             $post_type_object = get_post_type_object( $post_type );
  241.  
  242.             /* If $front has been set, add it to the $path. */
  243.             if ( 'post' == $post_type || 'attachment' == $post_type || ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front ) )
  244.                 $path .= trailingslashit( $wp_rewrite->front );
  245.  
  246.             /* If there's a slug, add it to the $path. */
  247.             if ( !empty( $post_type_object->rewrite['slug'] ) )
  248.                 $path .= $post_type_object->rewrite['slug'];
  249.  
  250.             /* If there's a path, check for parents. */
  251.             if ( !empty( $path ) )
  252.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( '', $path ) );
  253.  
  254.             /* If there's an archive page, add it to the trail. */
  255.             if ( !empty( $post_type_object->has_archive ) && function_exists( 'get_post_type_archive_link' ) )
  256.                 $trail[] = '<a href="' . get_post_type_archive_link( $post_type ) . '" title="' . esc_attr( $post_type_object->labels->name ) . '">' . $post_type_object->labels->name . '</a>';
  257.         }
  258.  
  259.         /* try to build a generic taxonomy trail no matter the post type and taxonomy and terms
  260.         $currentTax = "";
  261.         foreach(get_taxonomies() as $tax)
  262.         {
  263.             $terms = get_the_term_list( $post_id, $tax, '', '$$$', '' );
  264.             echo"<pre>";
  265.             print_r($tax.$terms);
  266.             echo"</pre>";
  267.         }
  268.         */
  269.  
  270.         /* If the post type path returns nothing and there is a parent, get its parents. */
  271.         if ( empty( $path ) && 0 !== $parent || 'attachment' == $post_type )
  272.             $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $parent, '' ) );
  273.  
  274.         /* Toggle the display of the posts page on single blog posts. */
  275.         if ( 'post' == $post_type && $show_posts_page == true && 'page' == get_option( 'show_on_front' ) ) {
  276.             $posts_page = get_option( 'page_for_posts' );
  277.             if ( $posts_page != '' && is_numeric( $posts_page ) ) {
  278.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( $posts_page, '' ) );
  279.             }
  280.         }
  281.  
  282.         if('post' == $post_type && $show_categories)
  283.         {
  284.                 $category = get_the_category();
  285.                
  286.                 foreach($category as $cat)
  287.                 {
  288.                     if(!empty($cat->parent))
  289.                     {
  290.                         $parents = get_category_parents($cat->cat_ID, TRUE, '$$$', FALSE );
  291.                         $parents = explode("$$$", $parents);
  292.                         foreach ($parents as $parent_item)
  293.                         {
  294.                             if($parent_item) $trail[] = $parent_item;
  295.                         }
  296.                         break;
  297.                     }
  298.                 }
  299.                
  300.                 if(isset($category[0]) && empty($parents))
  301.                 {
  302.                     $trail[] = '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
  303.                 }
  304.                
  305.         }
  306.  
  307.         if($post_type == 'portfolio')
  308.         {
  309.             $parents = get_the_term_list( $post_id, 'portfolio_entries', '', '$$$', '' );
  310.             $parents = explode('$$$',$parents);
  311.             foreach ($parents as $parent_item)
  312.             {
  313.                 if($parent_item) $trail[] = $parent_item;
  314.             }
  315.         }
  316.  
  317.         /* Display terms for specific post type taxonomy if requested. */
  318.         if ( isset( $args["singular_{$post_type}_taxonomy"] ) && $terms = get_the_term_list( $post_id, $args["singular_{$post_type}_taxonomy"], '', ', ', '' ) )
  319.             $trail[] = $terms;
  320.  
  321.         /* End with the post title. */
  322.         $post_title = get_the_title( $post_id ); // Force the post_id to make sure we get the correct page title.
  323.         if ( !empty( $post_title ) )
  324.             $trail['trail_end'] = $post_title;
  325.  
  326.     }
  327.  
  328.     /* If we're viewing any type of archive. */
  329.     elseif ( is_archive() ) {
  330.  
  331.         /* If viewing a taxonomy term archive. */
  332.         if ( is_tax() || is_category() || is_tag() ) {
  333.  
  334.             /* Get some taxonomy and term variables. */
  335.             $term = $wp_query->get_queried_object();
  336.             $taxonomy = get_taxonomy( $term->taxonomy );
  337.  
  338.             /* Get the path to the term archive. Use this to determine if a page is present with it. */
  339.             if ( is_category() )
  340.                 $path = 'Thema ' . get_option( 'category_base' );
  341.             elseif ( is_tag() )
  342.                 $path = 'Schlagwort ' . get_option( 'tag_base' );
  343.             else {
  344.                 if ( $taxonomy->rewrite['with_front'] && $wp_rewrite->front )
  345.                     $path = trailingslashit( $wp_rewrite->front );
  346.                 $path .= $taxonomy->rewrite['slug'];
  347.             }
  348.  
  349.             /* Get parent pages by path if they exist. */
  350.             if ( $path )
  351.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( '', $path ) );
  352.  
  353.             /* If the taxonomy is hierarchical, list its parent terms. */
  354.             if ( is_taxonomy_hierarchical( $term->taxonomy ) && $term->parent )
  355.                 $trail = array_merge( $trail, avia_breadcrumbs_get_term_parents( $term->parent, $term->taxonomy ) );
  356.  
  357.             /* Add the term name to the trail end. */
  358.             $trail['trail_end'] = $term->name;
  359.         }
  360.  
  361.         /* If viewing a post type archive. */
  362.         elseif ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) {
  363.  
  364.             /* Get the post type object. */
  365.             $post_type_object = get_post_type_object( get_query_var( 'post_type' ) );
  366.  
  367.             /* If $front has been set, add it to the $path. */
  368.             if ( $post_type_object->rewrite['with_front'] && $wp_rewrite->front )
  369.                 $path .= trailingslashit( $wp_rewrite->front );
  370.  
  371.             /* If there's a slug, add it to the $path. */
  372.             if ( !empty( $post_type_object->rewrite['archive'] ) )
  373.                 $path .= $post_type_object->rewrite['archive'];
  374.  
  375.             /* If there's a path, check for parents. */
  376.             if ( !empty( $path ) )
  377.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( '', $path ) );
  378.  
  379.             /* Add the post type [plural] name to the trail end. */
  380.             $trail['trail_end'] = $post_type_object->labels->name;
  381.         }
  382.  
  383.         /* If viewing an author archive. */
  384.         elseif ( is_author() ) {
  385.  
  386.             /* If $front has been set, add it to $path. */
  387.             if ( !empty( $wp_rewrite->front ) )
  388.                 $path .= trailingslashit( $wp_rewrite->front );
  389.  
  390.             /* If an $author_base exists, add it to $path. */
  391.             if ( !empty( $wp_rewrite->author_base ) )
  392.                 $path .= $wp_rewrite->author_base;
  393.  
  394.             /* If $path exists, check for parent pages. */
  395.             if ( !empty( $path ) )
  396.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( '', $path ) );
  397.  
  398.             /* Add the author's display name to the trail end. */
  399.             $trail['trail_end'] =  apply_filters('avf_author_name', get_the_author_meta('display_name', get_query_var('author')), get_query_var('author'));
  400.         }
  401.  
  402.         /* If viewing a time-based archive. */
  403.         elseif ( is_time() ) {
  404.  
  405.             if ( get_query_var( 'minute' ) && get_query_var( 'hour' ) )
  406.                 $trail['trail_end'] = get_the_time( __( 'g:i a', 'avia_framework' ) );
  407.  
  408.             elseif ( get_query_var( 'minute' ) )
  409.                 $trail['trail_end'] = sprintf( __( 'Minute %1$s', 'avia_framework' ), get_the_time( __( 'i', 'avia_framework' ) ) );
  410.  
  411.             elseif ( get_query_var( 'hour' ) )
  412.                 $trail['trail_end'] = get_the_time( __( 'g a', 'avia_framework' ) );
  413.         }
  414.  
  415.         /* If viewing a date-based archive. */
  416.         elseif ( is_date() ) {
  417.  
  418.             /* If $front has been set, check for parent pages. */
  419.             if ( $wp_rewrite->front )
  420.                 $trail = array_merge( $trail, avia_breadcrumbs_get_parents( '', $wp_rewrite->front ) );
  421.  
  422.             if ( is_day() ) {
  423.                 $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'avia_framework' ) ) . '">' . get_the_time( __( 'Y', 'avia_framework' ) ) . '</a>';
  424.                 $trail[] = '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '" title="' . get_the_time( esc_attr__( 'F', 'avia_framework' ) ) . '">' . get_the_time( __( 'F', 'avia_framework' ) ) . '</a>';
  425.                 $trail['trail_end'] = get_the_time( __( 'j', 'avia_framework' ) );
  426.             }
  427.  
  428.             elseif ( get_query_var( 'w' ) ) {
  429.                 $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'avia_framework' ) ) . '">' . get_the_time( __( 'Y', 'avia_framework' ) ) . '</a>';
  430.                 $trail['trail_end'] = sprintf( __( 'Week %1$s', 'avia_framework' ), get_the_time( esc_attr__( 'W', 'avia_framework' ) ) );
  431.             }
  432.  
  433.             elseif ( is_month() ) {
  434.                 $trail[] = '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '" title="' . get_the_time( esc_attr__( 'Y', 'avia_framework' ) ) . '">' . get_the_time( __( 'Y', 'avia_framework' ) ) . '</a>';
  435.                 $trail['trail_end'] = get_the_time( __( 'F', 'avia_framework' ) );
  436.             }
  437.  
  438.             elseif ( is_year() ) {
  439.                 $trail['trail_end'] = get_the_time( __( 'Y', 'avia_framework' ) );
  440.             }
  441.         }
  442.     }
  443.  
  444.     /* If viewing search results. */
  445.     elseif ( is_search() )
  446.         $trail['trail_end'] = sprintf( __( 'Search results for &quot;%1$s&quot;', 'avia_framework' ), esc_attr( get_search_query() ) );
  447.  
  448.     /* If viewing a 404 error page. */
  449.     elseif ( is_404() )
  450.         $trail['trail_end'] = __( '404 Not Found', 'avia_framework' );
  451.  
  452.     /* Allow child themes/plugins to filter the trail array. */
  453.     $trail = apply_filters( 'avia_breadcrumbs_trail', $trail, $args );
  454.  
  455.     /* Connect the breadcrumb trail if there are items in the trail. */
  456.     if ( is_array( $trail ) ) {
  457.  
  458.         $el_tag = "span";
  459.         $vocabulary = "";
  460.  
  461.         //google rich snippets
  462.         if($richsnippet === true)
  463.         {
  464.                     if(is_ssl()){
  465.                         $vocabulary = 'xmlns:v="https://rdf.data-vocabulary.org/#"';    
  466.                     }
  467.             else{
  468.                 $vocabulary = 'xmlns:v="http://rdf.data-vocabulary.org/#"';
  469.             }
  470.         }
  471.  
  472.         /* Open the breadcrumb trail containers. */
  473.         $breadcrumb = '<div class="breadcrumb breadcrumbs avia-breadcrumbs"><div class="breadcrumb-trail" '.$vocabulary.'>';
  474.  
  475.         /* If $before was set, wrap it in a container. */
  476.         if ( !empty( $before ) )
  477.             $breadcrumb .= '<'.$el_tag.' class="trail-before">' . $before . '</'.$el_tag.'> ';
  478.  
  479.         /* Wrap the $trail['trail_end'] value in a container. */
  480.         if ( !empty( $trail['trail_end'] ) )
  481.         {
  482.             if(!is_search())
  483.             {
  484.                 $trail['trail_end'] =  avia_backend_truncate($trail['trail_end'], $truncate, " ", $pad="...", false, '<strong><em><span>', true);
  485.             }
  486.             $trail['trail_end'] = '<'.$el_tag.' class="trail-end">' . $trail['trail_end'] . '</'.$el_tag.'>';
  487.         }
  488.  
  489.         if($richsnippet === true)
  490.         {
  491.             foreach($trail as $key => &$link)
  492.             {
  493.                 if("trail_end" == $key) continue;
  494.  
  495.                 $link = preg_replace('!rel=".+?"|rel=\'.+?\'|!',"", $link);
  496.                 $link = str_replace('<a ', '<a rel="v:url" property="v:title" ', $link);
  497.                 //$link = '<span typeof="v:Breadcrumb">'.$link.'</span>'; //removed due to data testing error
  498.                 $link = '<span>'.$link.'</span>';
  499.             }
  500.         }
  501.  
  502.  
  503.         /* Format the separator. */
  504.         if ( !empty( $separator ) )
  505.             $separator = '<span class="sep">' . $separator . '</span>';
  506.  
  507.         /* Join the individual trail items into a single string. */
  508.         $breadcrumb .= join( " {$separator} ", $trail );
  509.  
  510.         /* If $after was set, wrap it in a container. */
  511.         if ( !empty( $after ) )
  512.             $breadcrumb .= ' <span class="trail-after">' . $after . '</span>';
  513.  
  514.         /* Close the breadcrumb trail containers. */
  515.         $breadcrumb .= '</div></div>';
  516.     }
  517.  
  518.     /* Allow developers to filter the breadcrumb trail HTML. */
  519.     $breadcrumb = apply_filters( 'avia_breadcrumbs', $breadcrumb );
  520.  
  521.     /* Output the breadcrumb. */
  522.     if ( $echo )
  523.         echo $breadcrumb;
  524.     else
  525.         return $breadcrumb;
  526.  
  527. } // End avia_breadcrumbs()
  528.  
  529. /*-----------------------------------------------------------------------------------*/
  530. /* avia_breadcrumbs_get_parents() - Retrieve the parents of the current page/post */
  531. /*-----------------------------------------------------------------------------------*/
  532. /**
  533.  * Gets parent pages of any post type or taxonomy by the ID or Path.  The goal of this function is to create
  534.  * a clear path back to home given what would normally be a "ghost" directory.  If any page matches the given
  535.  * path, it'll be added.  But, it's also just a way to check for a hierarchy with hierarchical post types.
  536.  *
  537.  * @since 3.7.0
  538.  * @param int $post_id ID of the post whose parents we want.
  539.  * @param string $path Path of a potential parent page.
  540.  * @return array $trail Array of parent page links.
  541.  */
  542. function avia_breadcrumbs_get_parents( $post_id = '', $path = '' ) {
  543.  
  544.     /* Set up an empty trail array. */
  545.     $trail = array();
  546.  
  547.     /* If neither a post ID nor path set, return an empty array. */
  548.     if ( empty( $post_id ) && empty( $path ) )
  549.         return $trail;
  550.  
  551.     /* If the post ID is empty, use the path to get the ID. */
  552.     if ( empty( $post_id ) ) {
  553.  
  554.         /* Get parent post by the path. */
  555.         $parent_page = get_page_by_path( $path );
  556.  
  557.         /* ********************************************************************
  558.         Modification: The above line won't get the parent page if
  559.         the post type slug or parent page path is not the full path as required
  560.         by get_page_by_path. By using get_page_with_title, the full parent
  561.         trail can be obtained. This may still be buggy for page names that use
  562.         characters or long concatenated names.
  563.         Author: Byron Rode
  564.         Date: 06 June 2011
  565.         ******************************************************************* */
  566.  
  567.         if( empty( $parent_page ) )
  568.                 // search on page name (single word)
  569.             $parent_page = get_page_by_title ( $path );
  570.  
  571.         if( empty( $parent_page ) )
  572.             // search on page title (multiple words)
  573.             $parent_page = get_page_by_title ( str_replace( array('-', '_'), ' ', $path ) );
  574.  
  575.         /* End Modification */
  576.  
  577.         /* If a parent post is found, set the $post_id variable to it. */
  578.         if ( !empty( $parent_page ) )
  579.             $post_id = $parent_page->ID;
  580.     }
  581.  
  582.     /* If a post ID and path is set, search for a post by the given path. */
  583.     if ( $post_id == 0 && !empty( $path ) ) {
  584.  
  585.         /* Separate post names into separate paths by '/'. */
  586.         $path = trim( $path, '/' );
  587.         preg_match_all( "/\/.*?\z/", $path, $matches );
  588.  
  589.         /* If matches are found for the path. */
  590.         if ( isset( $matches ) ) {
  591.  
  592.             /* Reverse the array of matches to search for posts in the proper order. */
  593.             $matches = array_reverse( $matches );
  594.  
  595.             /* Loop through each of the path matches. */
  596.             foreach ( $matches as $match ) {
  597.  
  598.                 /* If a match is found. */
  599.                 if ( isset( $match[0] ) ) {
  600.  
  601.                     /* Get the parent post by the given path. */
  602.                     $path = str_replace( $match[0], '', $path );
  603.                     $parent_page = get_page_by_path( trim( $path, '/' ) );
  604.  
  605.                     /* If a parent post is found, set the $post_id and break out of the loop. */
  606.                     if ( !empty( $parent_page ) && $parent_page->ID > 0 ) {
  607.                         $post_id = $parent_page->ID;
  608.                         break;
  609.                     }
  610.                 }
  611.             }
  612.         }
  613.     }
  614.  
  615.     /* While there's a post ID, add the post link to the $parents array. */
  616.     while ( $post_id ) {
  617.  
  618.         /* Get the post by ID. */
  619.         $page = get_page( $post_id );
  620.  
  621.         /* Add the formatted post link to the array of parents. */
  622.         $parents[]  = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . get_the_title( $post_id ) . '</a>';
  623.  
  624.         /* Set the parent post's parent to the post ID. */
  625.         if(is_object($page))
  626.         {
  627.             $post_id = $page->post_parent;
  628.         }
  629.         else
  630.         {
  631.             $post_id = "";
  632.         }
  633.     }
  634.  
  635.     /* If we have parent posts, reverse the array to put them in the proper order for the trail. */
  636.     if ( isset( $parents ) )
  637.         $trail = array_reverse( $parents );
  638.  
  639.     /* Return the trail of parent posts. */
  640.     return $trail;
  641.  
  642. } // End avia_breadcrumbs_get_parents()
  643.  
  644. /*-----------------------------------------------------------------------------------*/
  645. /* avia_breadcrumbs_get_term_parents() - Retrieve the parents of the current term */
  646. /*-----------------------------------------------------------------------------------*/
  647. /**
  648.  * Searches for term parents of hierarchical taxonomies.  This function is similar to the WordPress
  649.  * function get_category_parents() but handles any type of taxonomy.
  650.  *
  651.  * @since 3.7.0
  652.  * @param int $parent_id The ID of the first parent.
  653.  * @param object|string $taxonomy The taxonomy of the term whose parents we want.
  654.  * @return array $trail Array of links to parent terms.
  655.  */
  656. function avia_breadcrumbs_get_term_parents( $parent_id = '', $taxonomy = '' ) {
  657.  
  658.     /* Set up some default arrays. */
  659.     $trail = array();
  660.     $parents = array();
  661.  
  662.     /* If no term parent ID or taxonomy is given, return an empty array. */
  663.     if ( empty( $parent_id ) || empty( $taxonomy ) )
  664.         return $trail;
  665.  
  666.     /* While there is a parent ID, add the parent term link to the $parents array. */
  667.     while ( $parent_id ) {
  668.  
  669.         /* Get the parent term. */
  670.         $parent = get_term( $parent_id, $taxonomy );
  671.  
  672.         /* Add the formatted term link to the array of parent terms. */
  673.         $parents[] = '<a href="' . get_term_link( $parent, $taxonomy ) . '" title="' . esc_attr( $parent->name ) . '">' . $parent->name . '</a>';
  674.  
  675.         /* Set the parent term's parent as the parent ID. */
  676.         $parent_id = $parent->parent;
  677.     }
  678.  
  679.     /* If we have parent terms, reverse the array to put them in the proper order for the trail. */
  680.     if ( !empty( $parents ) )
  681.         $trail = array_reverse( $parents );
  682.  
  683.     /* Return the trail of parent terms. */
  684.     return $trail;
  685.  
  686. } // End avia_breadcrumbs_get_term_parents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement