<?php
/*
 * eminent Breadcrumbs
*/
function eminent_custom_breadcrumbs() {
  $eminent_showonhome = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show   
  $eminent_showcurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show  
  global $post;
  
  if (is_home() || is_front_page()) {
    if ($eminent_showonhome == 1) echo '<li><a href="' . esc_url(home_url('/')) . '">' . esc_html__('Home','eminent') . '</a></li>';
  }  else {
    echo '<li><a href="' . esc_url(home_url('/')) . '">' . esc_html__('Home','eminent') . '</a> ' ;
   if ( is_category() ) {
      $eminent_thisCat = get_category(get_query_var('cat'), false);
      if ($eminent_thisCat->parent != 0) echo wp_kses_post( get_category_parents($eminent_thisCat->parent, TRUE, ' ' ) );      
		 esc_html_e('Category','eminent'); echo ' "'.esc_html(single_cat_title('', false)) . '"';
    } 
    elseif ( is_search() ) {
       esc_html_e('Search Results For','eminent'); echo ' "'. esc_html(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' ) {
        $eminent_post_type = get_post_type_object(get_post_type());
        $eminent_slug = $eminent_post_type->rewrite;
        echo '<a href="' . esc_url(home_url('/'.$eminent_slug['slug'].'/')) .'">' . esc_html($eminent_post_type->labels->singular_name) . '</a>';
        if ($eminent_showcurrent == 1) echo  esc_attr(get_the_title()) ;
      } else {
        $eminent_cat = get_the_category(); $eminent_cat = $eminent_cat[0];
        $eminent_cats = get_category_parents($eminent_cat, TRUE, '  ');
        if ($eminent_showcurrent == 0) $eminent_cats = preg_replace("#^(.+)\s\s$#", "$1", $eminent_cats);
        echo wp_kses_post( $eminent_cats );
        if ($eminent_showcurrent == 1) echo  esc_attr(get_the_title());
      }
    } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
      $eminent_post_type = get_post_type_object(get_post_type());
      echo  esc_html($eminent_post_type->labels->singular_name);
    } elseif ( is_attachment() ) {
      $eminent_parent = get_post($post->post_parent);
      $eminent_cat = get_the_category($eminent_parent->ID); $eminent_cat = $eminent_cat[0];
      echo wp_kses_post( get_category_parents($eminent_cat, TRUE, '  ') );
      echo '<a href="' . esc_url(get_permalink($eminent_parent)) . '">' . esc_html($eminent_parent->post_title) . '</a>';
      if ($eminent_showcurrent == 1) echo   esc_attr(get_the_title());
    } elseif ( is_page() && !$post->post_parent ) {
      if ($eminent_showcurrent == 1) echo  esc_attr(get_the_title());
    } elseif ( is_page() && $post->post_parent ) {
      $eminent_parent_id  = $post->post_parent;
      $eminent_breadcrumbs = array();
      while ($eminent_parent_id) {
        $eminent_page = get_page($eminent_parent_id);
        $eminent_breadcrumbs[] = '<a href="' . esc_url(get_permalink()) . '">' . esc_attr(get_the_title($eminent_page->ID)) . '</a>';
        $eminent_parent_id  = $eminent_page->post_parent;
      }
      $eminent_breadcrumbs = array_reverse($eminent_breadcrumbs);
      for ($eminent_i = 0; $eminent_i < count($eminent_breadcrumbs); $eminent_i++) {
        echo wp_kses_post( $eminent_breadcrumbs[$eminent_i] );
        if ($eminent_i != count($eminent_breadcrumbs)-1) echo '  ';
      }
      if ($eminent_showcurrent == 1) echo esc_attr(get_the_title());
    } elseif ( is_tag() ) {
       esc_html_e('Posts tagged','eminent'); echo ' "'.  single_tag_title('', false) . '"';
    } elseif ( is_author() ) {
       global $author;
      $eminent_userdata = get_userdata($author);
       esc_html_e('Articles posted by ','eminent'); echo esc_html($eminent_userdata->display_name) ;
    } elseif ( is_404() ) {
       esc_html_e('Error 404','eminent'); 
    }
    if ( intval( get_query_var('paged') ) ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo esc_html__('Page','eminent') . ' ' . intval( get_query_var('paged') );
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }
    echo '</li>';
  }
} ?>