woocommerce/templates/global/breadcrumb.php

222 lines
7.2 KiB
PHP
Raw Normal View History

2011-12-12 11:35:54 +00:00
<?php
/**
2012-08-14 18:05:45 +00:00
* Shop breadcrumb
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.2.0
* @see woocommerce_breadcrumb()
2011-12-12 11:35:54 +00:00
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $wp_query, $author;
2011-12-12 11:35:54 +00:00
$prepend = '';
$permalinks = get_option( 'woocommerce_permalinks' );
$shop_page_id = wc_get_page_id( 'shop' );
$shop_page = get_post( $shop_page_id );
// If permalinks contain the shop page in the URI prepend the breadcrumb with shop
if ( $shop_page_id && $shop_page && strstr( $permalinks['product_base'], '/' . $shop_page->post_name ) && get_option( 'page_on_front' ) != $shop_page_id ) {
$prepend = $before . '<a href="' . get_permalink( $shop_page ) . '">' . $shop_page->post_title . '</a> ' . $after . $delimiter;
}
2011-12-12 11:35:54 +00:00
if ( ( ! is_front_page() && ! ( is_post_type_archive() && get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) ) || is_paged() ) {
2012-08-14 18:05:45 +00:00
echo $wrap_before;
if ( ! empty( $home ) ) {
2013-02-08 17:45:39 +00:00
echo $before . '<a class="home" href="' . apply_filters( 'woocommerce_breadcrumb_home_url', home_url() ) . '">' . $home . '</a>' . $after . $delimiter;
}
2012-08-14 18:05:45 +00:00
if ( is_home() ) {
echo $before . single_post_title('', false) . $after;
2014-08-22 11:12:26 +00:00
} elseif ( is_category() ) {
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
$cat_obj = $wp_query->get_queried_object();
2012-08-25 11:08:03 +00:00
$this_category = get_category( $cat_obj->term_id );
2012-11-27 16:22:47 +00:00
2014-07-30 16:13:23 +00:00
if ( 0 != $this_category->parent ) {
$parent_category = get_category( $this_category->parent );
if ( ( $parents = get_category_parents( $parent_category, TRUE, $after . $delimiter . $before ) ) && ! is_wp_error( $parents ) ) {
echo $before . rtrim( $parents, $after . $delimiter . $before ) . $after . $delimiter;
}
2012-08-25 11:08:03 +00:00
}
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
echo $before . single_cat_title( '', false ) . $after;
2012-08-14 18:05:45 +00:00
2014-01-23 04:36:24 +00:00
} elseif ( is_tax( 'product_cat' ) ) {
2012-08-14 18:05:45 +00:00
echo $prepend;
2012-08-14 18:05:45 +00:00
$current_term = $wp_query->get_queried_object();
2013-04-08 19:10:55 +00:00
$ancestors = array_reverse( get_ancestors( $current_term->term_id, 'product_cat' ) );
2013-04-08 19:10:55 +00:00
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
2013-04-08 19:10:55 +00:00
echo $before . '<a href="' . get_term_link( $ancestor ) . '">' . esc_html( $ancestor->name ) . '</a>' . $after . $delimiter;
2012-08-25 11:08:03 +00:00
}
2012-08-14 18:05:45 +00:00
2013-04-08 19:10:55 +00:00
echo $before . esc_html( $current_term->name ) . $after;
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
} elseif ( is_tax( 'product_tag' ) ) {
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
$queried_object = $wp_query->get_queried_object();
2012-10-16 09:45:33 +00:00
echo $prepend . $before . __( 'Products tagged &ldquo;', 'woocommerce' ) . $queried_object->name . '&rdquo;' . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_day() ) {
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
echo $before . '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $after . $delimiter;
echo $before . '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a>' . $after . $delimiter;
echo $before . get_the_time( 'd' ) . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_month() ) {
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
echo $before . '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a>' . $after . $delimiter;
echo $before . get_the_time( 'F' ) . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_year() ) {
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
echo $before . get_the_time( 'Y' ) . $after;
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
} elseif ( is_post_type_archive( 'product' ) && get_option( 'page_on_front' ) !== $shop_page_id ) {
2012-08-14 18:05:45 +00:00
$_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : '';
2012-11-27 16:22:47 +00:00
2012-10-18 09:33:02 +00:00
if ( ! $_name ) {
$product_post_type = get_post_type_object( 'product' );
$_name = $product_post_type->labels->singular_name;
}
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
if ( is_search() ) {
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
echo $before . '<a href="' . get_post_type_archive_link( 'product' ) . '">' . $_name . '</a>' . $delimiter . __( 'Search results for &ldquo;', 'woocommerce' ) . get_search_query() . '&rdquo;' . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_paged() ) {
2012-08-14 18:05:45 +00:00
2014-01-15 05:53:37 +00:00
echo $before . '<a href="' . get_post_type_archive_link( 'product' ) . '">' . $_name . '</a>' . $after;
2012-08-25 11:08:03 +00:00
} else {
2012-11-27 16:22:47 +00:00
2012-08-25 10:25:43 +00:00
echo $before . $_name . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
}
2012-08-14 18:05:45 +00:00
2013-04-08 19:10:55 +00:00
} elseif ( is_single() && ! is_attachment() ) {
2012-08-14 18:05:45 +00:00
if ( 'product' == get_post_type() ) {
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
echo $prepend;
2012-08-14 18:05:45 +00:00
2014-07-30 16:13:23 +00:00
if ( $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ) ) {
2013-04-08 19:10:55 +00:00
$main_term = $terms[0];
2013-05-01 14:38:40 +00:00
$ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
2013-04-08 19:10:55 +00:00
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
2012-11-27 16:22:47 +00:00
if ( ! is_wp_error( $ancestor ) && $ancestor ) {
echo $before . '<a href="' . get_term_link( $ancestor ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
}
2012-08-25 11:08:03 +00:00
}
2012-11-27 16:22:47 +00:00
echo $before . '<a href="' . get_term_link( $main_term ) . '">' . $main_term->name . '</a>' . $after . $delimiter;
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
}
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
echo $before . get_the_title() . $after;
} elseif ( 'post' != get_post_type() ) {
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
$post_type = get_post_type_object( get_post_type() );
$slug = $post_type->rewrite;
echo $before . '<a href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type->labels->singular_name . '</a>' . $after . $delimiter;
2012-08-25 11:08:03 +00:00
echo $before . get_the_title() . $after;
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
} else {
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
$cat = current( get_the_category() );
if ( ( $parents = get_category_parents( $cat, TRUE, $after . $delimiter . $before ) ) && ! is_wp_error( $parents ) ) {
echo $before . rtrim( $parents, $after . $delimiter . $before ) . $after . $delimiter;
}
2012-08-25 11:08:03 +00:00
echo $before . get_the_title() . $after;
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
}
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_404() ) {
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
echo $before . __( 'Error 404', 'woocommerce' ) . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( ! is_single() && ! is_page() && get_post_type() != 'post' ) {
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
$post_type = get_post_type_object( get_post_type() );
2012-11-27 16:22:47 +00:00
if ( $post_type ) {
2012-08-25 11:08:03 +00:00
echo $before . $post_type->labels->singular_name . $after;
}
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_attachment() ) {
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
$parent = get_post( $post->post_parent );
2012-11-27 16:22:47 +00:00
$cat = get_the_category( $parent->ID );
2012-08-25 11:08:03 +00:00
$cat = $cat[0];
if ( ( $parents = get_category_parents( $cat, TRUE, $after . $delimiter . $before ) ) && ! is_wp_error( $parents ) ) {
echo $before . rtrim( $parents, $after . $delimiter . $before ) . $after . $delimiter;
}
2012-08-25 11:08:03 +00:00
echo $before . '<a href="' . get_permalink( $parent ) . '">' . $parent->post_title . '</a>' . $after . $delimiter;
2011-12-12 11:35:54 +00:00
echo $before . get_the_title() . $after;
2012-08-14 18:05:45 +00:00
2014-07-30 16:13:23 +00:00
} elseif ( is_page() && ! $post->post_parent ) {
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
echo $before . get_the_title() . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_page() && $post->post_parent ) {
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
$parent_id = $post->post_parent;
$breadcrumbs = array();
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
while ( $parent_id ) {
2014-10-05 22:27:52 +00:00
$page = get_post( $parent_id );
2014-08-22 11:12:26 +00:00
$breadcrumbs[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
$parent_id = $page->post_parent;
2011-12-12 11:35:54 +00:00
}
2012-11-27 16:22:47 +00:00
2012-08-25 11:08:03 +00:00
$breadcrumbs = array_reverse( $breadcrumbs );
2012-11-27 16:22:47 +00:00
foreach ( $breadcrumbs as $crumb ) {
2014-08-22 11:12:26 +00:00
echo $before . $crumb . $after . $delimiter;
}
2012-08-25 11:08:03 +00:00
2011-12-12 11:35:54 +00:00
echo $before . get_the_title() . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_search() ) {
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
echo $before . __( 'Search results for &ldquo;', 'woocommerce' ) . get_search_query() . '&rdquo;' . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_tag() ) {
2012-08-14 18:05:45 +00:00
echo $before . __( 'Posts tagged &ldquo;', 'woocommerce' ) . single_tag_title( '', false ) . '&rdquo;' . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
} elseif ( is_author() ) {
2012-08-14 18:05:45 +00:00
$userdata = get_userdata( $author );
2012-08-25 11:08:03 +00:00
echo $before . __( 'Author:', 'woocommerce' ) . ' ' . $userdata->display_name . $after;
2012-08-14 18:05:45 +00:00
2012-08-25 11:08:03 +00:00
}
2012-08-14 18:05:45 +00:00
if ( get_query_var( 'paged' ) ) {
2012-08-25 11:08:03 +00:00
echo ' (' . __( 'Page', 'woocommerce' ) . ' ' . get_query_var( 'paged' ) . ')';
}
2012-08-14 18:05:45 +00:00
2011-12-12 11:35:54 +00:00
echo $wrap_after;
}