-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-breadcrumbs.php
113 lines (97 loc) · 2.33 KB
/
get-breadcrumbs.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
<?php
/**
* Breadcrumbs
*
* @package uri-modern
*/
/**
* Call the breadcrumbs
*/
function uri_modern_breadcrumbs() {
$crumbs = array();
$prepend = explode( "\n", get_option( 'uri-modern-breadcrumbs', 'The University of Rhode Island (https://www.uri.edu)' ) );
foreach ( $prepend as $l ) {
$bits = explode( '(', $l );
$name = trim( $bits[0] );
$href = rtrim( $bits[1], ')' );
if ( ! empty( $name ) && ! empty( $href ) ) {
$crumbs[] = array(
'name' => $name,
'href' => $href,
);
}
}
$crumbs[] = array(
'name' => get_bloginfo(),
'href' => get_site_url(),
);
$url = uri_modern_get_current_path();
$path_segments = explode( '/', $url );
$path = '';
foreach ( $path_segments as $p ) {
if ( empty( $p ) ) {
continue;
}
$path = $path . '/' . $p;
$link = uri_modern_breadcrumbs_get_link( $path );
if ( null != $link ) {
$crumbs[] = $link;
}
}
return uri_modern_format_breadcrumbs( $crumbs );
}
/**
* WP lets us hunt and peck to see what the URL might be
*
* @param str $path the path.
*/
function uri_modern_breadcrumbs_get_link( $path ) {
$p = '';
$post_id = url_to_postid( $path );
if ( 0 !== $post_id ) { // it's a post or a page.
$p = get_page_by_path( $path );
$output = array(
'name' => get_the_title( $p->ID ),
'href' => get_site_url() . $path,
);
return $output;
}
// is it a category?
// @todo Catch custom post type categories too.
$category = get_category_by_path( $path );
if ( is_object( $category ) && isset( $category->term_id ) ) {
$output = array(
'name' => $category->name,
'href' => get_site_url() . '/' . $category->slug,
);
return $output;
}
// is it a custom post type?
$slug = get_post_type_object( get_post_type() )->rewrite['slug'];
if ( $slug ) {
$output = array(
'name' => ucfirst( $slug ),
'href' => get_site_url() . $path,
);
return $output;
}
}
/**
* Format the breadcrumbs
*
* @param arr $crumbs the crumbs.
* @return $output
*/
function uri_modern_format_breadcrumbs( $crumbs ) {
$output = '<ol>';
$last = end( $crumbs );
foreach ( $crumbs as $k => $c ) {
if ( $c === $last ) { // last crumb isn't a hyperlink.
$output .= '<li>' . $c['name'] . '</li>';
} else {
$output .= '<li><a href="' . $c['href'] . '">' . $c['name'] . '</a></li>';
}
}
$output .= '</ol>';
return $output;
}