├── README.md
├── calling-function.php
└── functions.php
/README.md:
--------------------------------------------------------------------------------
1 | # WP BreadCrumb Function
2 | Paste the `functions.php` code in your function file and call that function where you want to display breadcrumb.
3 |
4 | #### V 1.1
5 |
6 | This function updated and now supported to `custom taxonomy`, `custom post type` and `parent/child pages`. Also updated `archive (day, month, year) part`
7 |
8 | #### V 1.0
9 |
10 | This function is not yet supported to WooCommerce or any other specific plugin. It just based on default WordPress pages like `Single, Archive, Search, 404 or Page`.
11 |
12 | This function also check the parent and child category and display it like below.
13 |
14 | `Parent Category > Child Category > SubChild Category ...`
15 |
--------------------------------------------------------------------------------
/calling-function.php:
--------------------------------------------------------------------------------
1 | '»',
18 | 'id' => 'ah-breadcrumb',
19 | 'classes' => 'ah-breadcrumb',
20 | 'home_title' => esc_html__( 'Home', '' )
21 | );
22 |
23 | $sep = '
'. esc_html( $defaults['seperator'] ) .'';
24 |
25 | // Start the breadcrumb with a link to your homepage
26 | echo '';
27 |
28 | // Creating home link
29 | echo '- '. esc_html( $defaults['home_title'] ) .'
' . $sep;
30 |
31 | if ( is_single() ) {
32 |
33 | // Get posts type
34 | $post_type = get_post_type();
35 |
36 | // If post type is not post
37 | if( $post_type != 'post' ) {
38 |
39 | $post_type_object = get_post_type_object( $post_type );
40 | $post_type_link = get_post_type_archive_link( $post_type );
41 |
42 | echo '- '. $post_type_object->labels->name .'
'. $sep;
43 |
44 | }
45 |
46 | // Get categories
47 | $category = get_the_category( $post->ID );
48 |
49 | // If category not empty
50 | if( !empty( $category ) ) {
51 |
52 | // Arrange category parent to child
53 | $category_values = array_values( $category );
54 | $get_last_category = end( $category_values );
55 | // $get_last_category = $category[count($category) - 1];
56 | $get_parent_category = rtrim( get_category_parents( $get_last_category->term_id, true, ',' ), ',' );
57 | $cat_parent = explode( ',', $get_parent_category );
58 |
59 | // Store category in $display_category
60 | $display_category = '';
61 | foreach( $cat_parent as $p ) {
62 | $display_category .= '- '. $p .'
' . $sep;
63 | }
64 |
65 | }
66 |
67 | // If it's a custom post type within a custom taxonomy
68 | $taxonomy_exists = taxonomy_exists( $custom_taxonomy );
69 |
70 | if( empty( $get_last_category ) && !empty( $custom_taxonomy ) && $taxonomy_exists ) {
71 |
72 | $taxonomy_terms = get_the_terms( $post->ID, $custom_taxonomy );
73 | $cat_id = $taxonomy_terms[0]->term_id;
74 | $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
75 | $cat_name = $taxonomy_terms[0]->name;
76 |
77 | }
78 |
79 | // Check if the post is in a category
80 | if( !empty( $get_last_category ) ) {
81 |
82 | echo $display_category;
83 | echo '- '. get_the_title() .'
';
84 |
85 | } else if( !empty( $cat_id ) ) {
86 |
87 | echo '- '. $cat_name .'
' . $sep;
88 | echo '- '. get_the_title() .'
';
89 |
90 | } else {
91 |
92 | echo '- '. get_the_title() .'
';
93 |
94 | }
95 |
96 | } else if( is_archive() ) {
97 |
98 | if( is_tax() ) {
99 | // Get posts type
100 | $post_type = get_post_type();
101 |
102 | // If post type is not post
103 | if( $post_type != 'post' ) {
104 |
105 | $post_type_object = get_post_type_object( $post_type );
106 | $post_type_link = get_post_type_archive_link( $post_type );
107 |
108 | echo '- ' . $post_type_object->labels->name . '
' . $sep;
109 |
110 | }
111 |
112 | $custom_tax_name = get_queried_object()->name;
113 | echo '- '. $custom_tax_name .'
';
114 |
115 | } else if ( is_category() ) {
116 |
117 | $parent = get_queried_object()->category_parent;
118 |
119 | if ( $parent !== 0 ) {
120 |
121 | $parent_category = get_category( $parent );
122 | $category_link = get_category_link( $parent );
123 |
124 | echo '- '. $parent_category->name .'
' . $sep;
125 |
126 | }
127 |
128 | echo '- '. single_cat_title( '', false ) .'
';
129 |
130 | } else if ( is_tag() ) {
131 |
132 | // Get tag information
133 | $term_id = get_query_var('tag_id');
134 | $taxonomy = 'post_tag';
135 | $args = 'include=' . $term_id;
136 | $terms = get_terms( $taxonomy, $args );
137 | $get_term_name = $terms[0]->name;
138 |
139 | // Display the tag name
140 | echo '- '. $get_term_name .'
';
141 |
142 | } else if( is_day() ) {
143 |
144 | // Day archive
145 |
146 | // Year link
147 | echo '- '. get_the_time('Y') . ' Archives
' . $sep;
148 |
149 | // Month link
150 | echo '- '. get_the_time('M') .' Archives
' . $sep;
151 |
152 | // Day display
153 | echo '- '. get_the_time('jS') .' '. get_the_time('M'). ' Archives
';
154 |
155 | } else if( is_month() ) {
156 |
157 | // Month archive
158 |
159 | // Year link
160 | echo '- '. get_the_time('Y') . ' Archives
' . $sep;
161 |
162 | // Month Display
163 | echo '- '. get_the_time('M') .' Archives
';
164 |
165 | } else if ( is_year() ) {
166 |
167 | // Year Display
168 | echo '- '. get_the_time('Y') .' Archives
';
169 |
170 | } else if ( is_author() ) {
171 |
172 | // Auhor archive
173 |
174 | // Get the author information
175 | global $author;
176 | $userdata = get_userdata( $author );
177 |
178 | // Display author name
179 | echo '- '. 'Author: '. $userdata->display_name . '
';
180 |
181 | } else {
182 |
183 | echo '- '. post_type_archive_title() .'
';
184 |
185 | }
186 |
187 | } else if ( is_page() ) {
188 |
189 | // Standard page
190 | if( $post->post_parent ) {
191 |
192 | // If child page, get parents
193 | $anc = get_post_ancestors( $post->ID );
194 |
195 | // Get parents in the right order
196 | $anc = array_reverse( $anc );
197 |
198 | // Parent page loop
199 | if ( !isset( $parents ) ) $parents = null;
200 | foreach ( $anc as $ancestor ) {
201 |
202 | $parents .= '- '. get_the_title( $ancestor ) .'
' . $sep;
203 |
204 | }
205 |
206 | // Display parent pages
207 | echo $parents;
208 |
209 | // Current page
210 | echo '- '. get_the_title() .'
';
211 |
212 | } else {
213 |
214 | // Just display current page if not parents
215 | echo '- '. get_the_title() .'
';
216 |
217 | }
218 |
219 | } else if ( is_search() ) {
220 |
221 | // Search results page
222 | echo '- Search results for: '. get_search_query() .'
';
223 |
224 | } else if ( is_404() ) {
225 |
226 | // 404 page
227 | echo '- ' . 'Error 404' . '
';
228 |
229 | }
230 |
231 | // End breadcrumb
232 | echo '
';
233 |
234 | }
235 |
--------------------------------------------------------------------------------