<?php
/*
 * relic Breadcrumbs
*/
function relic_custom_breadcrumbs() {
  $relic_showonhome = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show  
  $relic_showcurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show

  global $post;
  if (is_home() || is_front_page()) {
    if ($relic_showonhome == 1) echo '<li id="breadcrumbs" ><a href="' . esc_url ( home_url('/') ). '">' . esc_html__('Home','relic') . '</a></li>';
    
  }  else {

    echo '<li id="breadcrumbs_home"><a href="' . esc_url ( home_url('/') ) . '">' . esc_html__('Home','relic') . '</a> ';
    
   if ( is_category() ) {
      $relic_thisCat = get_category(get_query_var('cat'), false);
      if ($relic_thisCat->parent != 0) echo get_category_parents($relic_thisCat->parent, TRUE, ' ');      
      esc_html_e('Category','relic'); echo ' "'.single_cat_title('', false) . '"' ;
    } 
    elseif ( is_search() ) {
       esc_html_e('Search Results For','relic'); echo ' "'. get_search_query() . '"';

    } elseif ( is_day() ) {
      echo '<a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . esc_html(get_the_time('Y')) . '</a> ';
      echo '<a href="' . esc_url(get_month_link(get_the_time('Y'),get_the_time('m'))) . '">' . esc_html(get_the_time('F')) . '</a>  ';
      echo  esc_html(get_the_time('d')) ;

    } elseif ( is_month() ) {
      echo '<a href="' . esc_url(get_year_link(get_the_time('Y'))) . '">' . esc_html(get_the_time('Y')) . '</a> ';
      echo  esc_html(get_the_time('F')) ;

    } elseif ( is_year() ) {
      echo  esc_html(get_the_time('Y')) ;

    } elseif ( is_single() && !is_attachment() ) {
      if ( get_post_type() != 'post' ) {
        $relic_post_type = get_post_type_object(get_post_type());
        $relic_slug = $relic_post_type->rewrite;
        echo '<a href="' . esc_url ( home_url('/'. $relic_slug['slug']) ) . '/' . $relic_slug['slug'] . '/">' . esc_html($relic_post_type->labels->singular_name) . '</a>';
        if ($relic_showcurrent == 1) echo  esc_attr(get_the_title()) ;
      } else {
        $relic_cat = get_the_category(); $relic_cat = $relic_cat[0];
        $relic_cats = get_category_parents($relic_cat, TRUE, ' ');
        if ($relic_showcurrent == 0) $relic_cats = preg_replace("#^(.+)\s \s$#", "$1", $relic_cats);
        echo $relic_cats;
        if ($relic_showcurrent == 1) echo  esc_attr(get_the_title()) ;
      }

    } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
      $relic_post_type = get_post_type_object(get_post_type());
      echo  esc_html($relic_post_type->labels->singular_name) ;

    } elseif ( is_attachment() ) {
      $relic_parent = get_post($post->post_parent);
      $relic_cat = get_the_category($relic_parent->ID); $relic_cat = $relic_cat[0];
      echo get_category_parents($relic_cat, TRUE, '  ');
      echo '<a href="' . esc_url(get_permalink($relic_parent)) . '">' . $relic_parent->post_title . '</a>';
      if ($relic_showcurrent == 1) echo  esc_attr(get_the_title()) ;

    } elseif ( is_page() && !$post->post_parent ) {
      if ($relic_showcurrent == 1) echo  esc_attr(get_the_title()) ;

    } elseif ( is_page() && $post->post_parent ) {
      $relic_parent_id  = $post->post_parent;
      $relic_breadcrumbs = array();
      while ($relic_parent_id) {
        $relic_page = get_page($relic_parent_id);
        $relic_breadcrumbs[] = '<a href="' . get_permalink($relic_page->ID) . '">' . esc_attr(get_the_title($relic_page->ID)) . '</a>';
        $relic_parent_id  = $relic_page->post_parent;
      }
      $relic_breadcrumbs = array_reverse($relic_breadcrumbs);
      for ($relic_i = 0; $relic_i < count($relic_breadcrumbs); $relic_i++) {
        echo $relic_breadcrumbs[$relic_i];
        if ($relic_i != count($relic_breadcrumbs)-1) echo ' ';
      }
      if ($relic_showcurrent == 1) echo esc_attr(get_the_title()) ;

    } elseif ( is_tag() ) {
       esc_html_e('Posts tagged','relic'); echo ' "'.  single_tag_title('', false) . '"' ;

    } elseif ( is_author() ) {
       global $author;
      $relic_userdata = get_userdata($author);
       esc_html_e('Articles posted by ','relic'); echo esc_html($relic_userdata->display_name) ;

    } elseif ( is_404() ) {
       esc_html_e('Error 404','relic'); 
    }
    
    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo esc_html__('Page','relic') . ' ' . esc_html(get_query_var('paged'));
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }
    echo '</li>';
  }
} ?>