Breadcrumbs trail is a useful and easy to understand secondary navigation tool for large websites, The breadcrumbs trail offers a visually way to your site visitor to keep track and know where they are.

The term breadcrumb trail comes from the story of Hansel and Gretel, who left a trail of breadcrumbs as they walked through the forest so they could trace their way back home.

We’ve created this function to use in our WordPress Themes even with custom post types and taxonomies, This simple function will generates locational breadcrumb trails for your WordPress powered blog or website.

You are free to use Mine Breadcrumbs trail in your WordPress themes for personal or commercial use, feedback and improvements are always welcome, simply copy this code below and paste it in your theme’s functions.php, have a question don’t hesitate leave us a comment.

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php // Mine Breadcrumbs trail Navigation function
function mine_breadcrumbs() {
	global $cat, $s, $post, $tag_id, $author, $wp_query;

	// if query for category then get current category
	if (get_the_category()) $category = get_the_category();
	
	// if query for tag then get current tag
	if (is_tag()) $tag = get_term($tag_id, 'post_tag', OBJECT, 'display');
	
	// if query for author then get current author
	if (is_author()) $userdata = get_userdata($author);
	
	// get query var post type
	$post_type = get_query_var('post_type');
	
	// asign a separator
	$separator = '<li class="separator"></li>'; ?>
	
	<ul id="breadcrumbs">
		<?php echo '<li>You are here: <a href="' .home_url() .'" title="' .get_bloginfo('name') .'">Home</a></li>';
		
		// Breadcrumb for category and sub-category archive
		if (is_category()) {
			echo $separator .'<li>';
			$cat_obj = $wp_query->get_queried_object();
			$thisCat = $cat_obj->term_id;
			$thisCat = get_category($thisCat);
			$parentCat = get_category($thisCat->parent);
			if ($thisCat->parent != 0) echo get_category_parents($parentCat, true, '</li>' .$separator .'<li>');
			echo 'Archive by Category - ' .single_cat_title() .'</li>';
		}
		
		// breadcrumbs for custom taxonomy terms
		elseif (is_tax()) {
			
			// get post taxonomy
			$taxonomy = get_query_var('taxonomy');
			$object_id = get_queried_object_id();
			$term = get_term($object_id, $taxonomy);
			
			if (!empty ($term)) {
				$term_name = $term->name;
				// get term link
				$term_link = get_term_link((int)$term->term_id, $taxonomy);
			}
			
			$term_parent = $term->parent;
			
			// get all parents terms
			while($term_parent) {
				$term = get_term($term_parent, $taxonomy);
				$term_parent = $term->parent;
				$term_parents[] = array('name' => $term->name, 'url' => get_term_link((int)$term->term_id, $taxonomy));
			}
			
			if(!empty($term_parents)){
				$term_parents = array_reverse($term_parents);
				
				foreach($term_parents as $term){
					$output .= $separator .'<li><a href="' .$term['url'] .'">' .$term['name'] .'</a></li>';
					echo $output;
				}
			}
			echo $separator .'<li>' .__('Archive for', THEME_NAME) .' ' .ucfirst(substr($taxonomy, 0, -1)) .' - ' .$term_name .'</li>';
		}
		
		// Breadcrumb for daily archives
		elseif (is_day()) {
			echo $separator .'<li><a href="' .get_year_link(get_the_time('Y')) .'">' .get_the_time('Y') .'</a></li>';
			echo $separator .'<li><a href="' .get_month_link(get_the_time('Y'), get_the_time('m')) .'">' .get_the_time('F') .'</a></li>';
			echo $separator .'<li>' .get_the_time('d') .'</li>';
		}
		
		// Breadcrumb for monthly archive
		elseif (is_month()) {
			echo $separator .'<li><a href="' .get_year_link(get_the_time('Y')) .'">' .get_the_time('Y') .'</a></li>';
			echo $separator .'<li>' .get_the_time('F') .'</li>';
		}
		
		// Breadcrumb for yearly archive
		elseif (is_year()) {
			echo $separator .'<li>' .get_the_time('Y') .'</li>';
		}
		
		// Breadcrumb for single post and attachments
		elseif (is_single() && !is_attachment()) {
			if (get_post_type() == $post_type ) {
				
				// get post taxonomy
				$taxonomies = get_object_taxonomies($post_type);
				foreach ($taxonomies as $taxonomy) {
					$taxonomy = $taxonomy;
				}
				
				$terms = get_the_terms( $post_id, $taxonomy );
				if (!empty ($terms)) {
					
					foreach ($terms as $term) {
						$term_name = $term->name;
						
						// get term link
						$term_link = get_term_link((int)$term->term_id, $taxonomy);
						
						// get term parent
						$term_parent = $term->parent;
					}
				}
				
				// get all parents terms
				while($term_parent) {
					$term = get_term($term_parent, $taxonomy);
					$term_parent = $term->parent;
					$term_parents[] = array('name' => $term->name, 'url' => get_term_link((int)$term->term_id, $taxonomy));
				}
				
				if (!empty($term_parents)) {
					$term_parents = array_reverse($term_parents);
					foreach($term_parents as $term) {
						$output .= $separator .'<li><a href="' .$term['url'] .'">' .$term['name'] .'</a></li>';
						echo $output;
					}
				}
				
				echo $separator .'<li><a href="' .$term_link .'">' .$term_name .'</a></li>';
				echo $separator .'<li>' .get_the_title($post->post_parent) .'</li>';
			
			} else {
				echo $separator .'<li>' .substr(get_category_parents($category[0]->cat_ID, true, '</li>' . $separator .'<li>'),0, -9) .'</li>';
				echo '<li>' .get_the_title($post->post_parent) .'</li>';
			}
		}
		
		// Breadcrumb for posts page (Blog page)
		elseif ((is_home()) && (!is_front_page())) { 
			echo $separator .'<li>' .get_the_title( get_option('page_for_posts') ) .'</li>';
		}
		
		// Breadcrumb for pages
		elseif ((is_page()) && (!is_front_page())) { 
			$ancestors = get_post_ancestors($post);
			if ($ancestors) {
				$ancestors = array_reverse($ancestors);
				foreach ($ancestors as $ancestor) {
					echo $separator .'<li><a href="' .get_permalink($ancestor) .'">' .get_the_title($ancestor) .'</a></li>';
				} 
			}
			echo $separator .'<li>' .get_the_title($post) .'</li>';
		}
		
		// Breadcrumb for error 404 page
		elseif (is_404()) {
			echo $separator .'<li> - ' .__('404 - Page Not Found', THEME_NAME) .'</li>';
		}
		
		// Breadcrumb for search result page
		elseif (is_search()) { 
			echo $separator .'<li>' .__('Search Results for', THEME_NAME) .' - ' .wp_specialchars(get_query_var('s')) .'</li>';
		}
		
		// Breadcrumb for tag archives
		elseif (is_tag()) { 
			echo $separator .'<li>' .__('Archive for Tag', THEME_NAME) .' - ' .$tag->name .'</li>';
		}
		
		// Breadcrumb for author archive
		elseif (is_author()) { 
			echo $separator .'<li>' .__('Articles Posted by', THEME_NAME) .' - ' .$userdata->display_name .'</li>';
		} ?>
	</ul>
<?php } ?>

Add this code below in theme’s template files where you want to show breadcrumbs navigation.

1
2
3
4
5
<?php // print breadcrumbs trail navigation
	mine_breadcrumbs();
?>