forked from ajulien-fr/bootstrap_breadcrumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-breadcrumb.php
131 lines (94 loc) · 4.07 KB
/
bootstrap-breadcrumb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* Retrieve category parents.
*
* @param int $id Category ID.
* @param array $visited Optional. Already linked to categories to prevent duplicates.
* @return string|WP_Error A list of category parents on success, WP_Error on failure.
*/
function custom_get_category_parents( $id, $visited = array() ) {
$chain = '';
$parent = get_term( $id, 'category' );
if ( is_wp_error( $parent ) )
return $parent;
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= custom_get_category_parents( $parent->parent, $visited );
}
$chain .= '<li class="breadcrumb-item"><a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $name. '</a>' . '</li>';
return $chain;
}
function bootstrap_breadcrumb() {
global $post;
$html = '<ol class="breadcrumb">';
if ( (is_front_page()) || (is_home()) ) {
$html .= '<li class="breadcrumb-item active">Home</li>';
}
else {
$html .= '<li class="breadcrumb-item"><a href="'.esc_url(home_url('/')).'">Home</a></li>';
if ( is_attachment() ) {
$parent = get_post($post->post_parent);
$categories = get_the_category($parent->ID);
if ( $categories[0] ) {
$html .= custom_get_category_parents($categories[0]);
}
$html .= '<li class="breadcrumb-item"><a href="' . esc_url( get_permalink( $parent ) ) . '">' . $parent->post_title . '</a></li>';
$html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
}
elseif ( is_category() ) {
$category = get_category( get_query_var( 'cat' ) );
if ( $category->parent != 0 ) {
$html .= custom_get_category_parents( $category->parent );
}
$html .= '<li class="breadcrumb-item active">' . single_cat_title( '', false ) . '</li>';
}
elseif ( is_page() && !is_front_page() ) {
$parent_id = $post->post_parent;
$parent_pages = array();
while ( $parent_id ) {
$page = get_page($parent_id);
$parent_pages[] = $page;
$parent_id = $page->post_parent;
}
$parent_pages = array_reverse( $parent_pages );
if ( !empty( $parent_pages ) ) {
foreach ( $parent_pages as $parent ) {
$html .= '<li class="breadcrumb-item"><a href="' . esc_url( get_permalink( $parent->ID ) ) . '">' . get_the_title( $parent->ID ) . '</a></li>';
}
}
$html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
}
elseif ( is_singular( 'post' ) ) {
$categories = get_the_category();
if ( $categories[0] ) {
$html .= custom_get_category_parents($categories[0]);
}
$html .= '<li class="breadcrumb-item active">' . get_the_title() . '</li>';
}
elseif ( is_tag() ) {
$html .= '<li class="breadcrumb-item active">' . single_tag_title( '', false ) . '</li>';
}
elseif ( is_day() ) {
$html .= '<li class="breadcrumb-item"><a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . get_the_time( 'Y' ) . '</a></li>';
$html .= '<li class="breadcrumb-item"><a href="' . esc_url( get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) ) . '">' . get_the_time( 'm' ) . '</a></li>';
$html .= '<li class="breadcrumb-item active">' . get_the_time('d') . '</li>';
}
elseif ( is_month() ) {
$html .= '<li class="breadcrumb-item"><a href="' . esc_url( get_year_link( get_the_time( 'Y' ) ) ) . '">' . get_the_time( 'Y' ) . '</a></li>';
$html .= '<li class="breadcrumb-item active">' . get_the_time( 'F' ) . '</li>';
}
elseif ( is_year() ) {
$html .= '<li class="breadcrumb-item active">' . get_the_time( 'Y' ) . '</li>';
}
elseif ( is_author() ) {
$html .= '<li class="breadcrumb-item active">' . get_the_author() . '</li>';
}
elseif ( is_search() ) {
}
elseif ( is_404() ) {
}
}
$html .= '</ol>';
echo $html;
}