Instantly share code, notes, and snippets.
Created
March 26, 2014 03:15
-
Save thanh4890/9776371 to your computer and use it in GitHub Desktop.
Support: Eliana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Template tag: print breadcrumb | |
*/ | |
function kopa_breadcrumb() { | |
// get show/hide option | |
$kopa_breadcrumb_status = get_option('kopa_theme_options_breadcrumb_status', 'show'); | |
if ( $kopa_breadcrumb_status != 'show' ) { | |
return; | |
} | |
if (is_main_query()) { | |
global $post, $wp_query; | |
$prefix = ' / '; | |
$current_class = 'current-page'; | |
$description = ''; | |
$breadcrumb_before = '<div class="breadcrumb clearfix">'; | |
$breadcrumb_after = '</div>'; | |
$breadcrumb_home = '<a href="' . home_url() . '">' . __('Home', kopa_get_domain()) . '</a>'; | |
$breadcrumb = ''; | |
?> | |
<?php | |
if (is_home()) { | |
$breadcrumb.= $breadcrumb_home; | |
if ( get_option( 'page_for_posts' ) ) { | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title(get_option('page_for_posts'))); | |
} else { | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, __('Blog', kopa_get_domain())); | |
} | |
} else if (is_post_type_archive('product') && get_option('woocommerce_shop_page_id')) { | |
$breadcrumb.= $breadcrumb_home; | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title(get_option('woocommerce_shop_page_id'))); | |
} else if (is_tag()) { | |
$breadcrumb.= $breadcrumb_home; | |
$term = get_term(get_queried_object_id(), 'post_tag'); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $term->name); | |
} else if (is_category()) { | |
$breadcrumb.= $breadcrumb_home; | |
$category_id = get_queried_object_id(); | |
$terms_link = explode(',', substr(get_category_parents(get_queried_object_id(), TRUE, ','), 0, (strlen(',') * -1))); | |
$n = count($terms_link); | |
if ($n > 1) { | |
for ($i = 0; $i < ($n - 1); $i++) { | |
$breadcrumb.= $prefix . $terms_link[$i]; | |
} | |
} | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_category_by_ID(get_queried_object_id())); | |
} else if ( is_tax('product_cat') ) { | |
$breadcrumb.= $breadcrumb_home; | |
$breadcrumb.= $prefix . '<a href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</a>'; | |
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); | |
$parents = array(); | |
$parent = $term->parent; | |
while ($parent): | |
$parents[] = $parent; | |
$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' )); | |
$parent = $new_parent->parent; | |
endwhile; | |
if( ! empty( $parents ) ): | |
$parents = array_reverse($parents); | |
foreach ($parents as $parent): | |
$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' )); | |
$breadcrumb .= $prefix . '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>'; | |
endforeach; | |
endif; | |
$queried_object = get_queried_object(); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $queried_object->name); | |
} else if ( is_tax( 'product_tag' ) ) { | |
$breadcrumb.= $breadcrumb_home; | |
$breadcrumb.= $prefix . '<a href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</a>'; | |
$queried_object = get_queried_object(); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $queried_object->name); | |
} else if (is_single()) { | |
$breadcrumb.= $breadcrumb_home; | |
if ( get_post_type() === 'product' ) : | |
$breadcrumb .= $prefix . '<a href="'.get_page_link( get_option('woocommerce_shop_page_id') ).'">'.get_the_title( get_option('woocommerce_shop_page_id') ).'</a>'; | |
if ($terms = get_the_terms( $post->ID, 'product_cat' )) : | |
$term = apply_filters( 'jigoshop_product_cat_breadcrumb_terms', current($terms), $terms); | |
$parents = array(); | |
$parent = $term->parent; | |
while ($parent): | |
$parents[] = $parent; | |
$new_parent = get_term_by( 'id', $parent, 'product_cat'); | |
$parent = $new_parent->parent; | |
endwhile; | |
if(!empty($parents)): | |
$parents = array_reverse($parents); | |
foreach ($parents as $parent): | |
$item = get_term_by( 'id', $parent, 'product_cat'); | |
$breadcrumb .= $prefix . '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>'; | |
endforeach; | |
endif; | |
$breadcrumb .= $prefix . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $term->name . '</a>'; | |
endif; | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title()); | |
else : | |
$categories = get_the_category(get_queried_object_id()); | |
if ($categories) { | |
foreach ($categories as $category) { | |
$breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_category_link($category->term_id), $category->name); | |
} | |
} | |
$post_id = get_queried_object_id(); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title($post_id)); | |
endif; | |
} else if (is_page()) { | |
if (!is_front_page()) { | |
$post_id = get_queried_object_id(); | |
$breadcrumb.= $breadcrumb_home; | |
$post_ancestors = get_post_ancestors($post); | |
if ($post_ancestors) { | |
$post_ancestors = array_reverse($post_ancestors); | |
foreach ($post_ancestors as $crumb) | |
$breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_permalink($crumb), get_the_title($crumb)); | |
} | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, get_the_title(get_queried_object_id())); | |
} | |
} else if (is_year() || is_month() || is_day()) { | |
$breadcrumb.= $breadcrumb_home; | |
$date = array('y' => NULL, 'm' => NULL, 'd' => NULL); | |
$date['y'] = get_the_time('Y'); | |
$date['m'] = get_the_time('m'); | |
$date['d'] = get_the_time('j'); | |
if (is_year()) { | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $date['y']); | |
} | |
if (is_month()) { | |
$breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_year_link($date['y']), $date['y']); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, date_i18n('F', $date['m'])); | |
} | |
if (is_day()) { | |
$breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_year_link($date['y']), $date['y']); | |
$breadcrumb.= $prefix . sprintf('<a href="%1$s">%2$s</a>', get_month_link($date['y'], $date['m']), date_i18n('F', $date['m'])); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, $date['d']); | |
} | |
} else if (is_search()) { | |
$breadcrumb.= $breadcrumb_home; | |
$s = get_search_query(); | |
$c = $wp_query->found_posts; | |
$description = sprintf(__('<span class="%1$s">Your search for "%2$s"', kopa_get_domain()), $current_class, $s); | |
$breadcrumb .= $prefix . $description; | |
} else if (is_author()) { | |
$breadcrumb.= $breadcrumb_home; | |
$author_id = get_queried_object_id(); | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</a>', $current_class, sprintf(__('Posts created by %1$s', kopa_get_domain()), get_the_author_meta('display_name', $author_id))); | |
} else if (is_404()) { | |
$breadcrumb.= $breadcrumb_home; | |
$breadcrumb.= $prefix . sprintf('<span class="%1$s">%2$s</span>', $current_class, __('Error 404', kopa_get_domain())); | |
} | |
if ($breadcrumb) | |
echo apply_filters('kopa_breadcrumb', $breadcrumb_before . $breadcrumb . $breadcrumb_after); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment