Хлебные крошки (breadcrumbs) – это навигационный метод отображения всех посещенных страниц, ведущие от домашней страницы до текущей страницы. Все страницы связаны между собой для легкой навигации в обратном направлении. Как правило, крошки находится в верхней части веб-страницы.

Есть множество плагинов WordPress, но они не всегда подходящий для вас вариант Этот код будет иметь хлебные крошки, которые сразу заработают вашем сайте.

Вставляем код в файл function.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Хлебные крошки
function custom_breadcrumbs() {
    // Настройки
    $separator = '>'; // Разделитель крошек
    $breadcrums_id = 'breadcrumbs'; // id блока
    $breadcrums_class = 'breadcrumbs'; // class крошек
    $home_title = 'Главная'; // Название главной страницы в хлебных крошках
    // Если у вас есть какие-либо типы таксономией, добавьте код в переменную ниже (например, product_cat для Woocommerce)
    $custom_taxonomy = 'product_cat';
    // Получаем запись и запрос
    global $post, $wp_query;
    // Не отображать на главной
    if (!is_front_page()) {
        // Начало хлебных крошек
        echo '<ul class="' . $breadcrums_class . '">
 	        <li style="list-style-type: none;">
                  <ul class="' . $breadcrums_class . '">';
        // Главная
        echo '<li class="item-home"><a class="bread-link bread-home" title="' . $home_title . '" href="' . get_home_url() . '">' . $home_title . '</a></li>
                  </ul>
                </li>
             </ul>';
        echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator separator-home">' . $separator . '</li>
</ul>
</li>
</ul>';
        if (is_archive() && !is_tax() && !is_category() && !is_tag()) {
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-archive"><strong class="bread-current bread-archive">' . post_type_archive_title($prefix, false) . '</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_archive() && is_tax() && !is_category() && !is_tag()) { // Если запись - пользовательський другой тип записи
            $post_type = get_post_type();
            if ($post_type != 'post') {
                $post_type_object = get_post_type_object($post_type);
                $post_type_archive = get_post_type_archive_link($post_type);
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" title="' . $post_type_object->labels->name . '" href="' . $post_type_archive . '">' . $post_type_object->labels->name . '</a></li>
</ul>
</li>
</ul>
';
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator">' . $separator . '</li>
</ul>
</li>
</ul>
';
            }
            $custom_tax_name = get_queried_object()->name;
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-archive"><strong class="bread-current bread-archive">' . $custom_tax_name . '</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_single()) { // Если запись - пользовательський другой тип записи
            $post_type = get_post_type();
            if ($post_type != 'post') {
                $post_type_object = get_post_type_object($post_type);
                $post_type_archive = get_post_type_archive_link($post_type);
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" title="' . $post_type_object->labels->name . '" href="' . $post_type_archive . '">' . $post_type_object->labels->name . '</a></li>
</ul>
</li>
</ul>
';
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator">' . $separator . '</li>
</ul>
</li>
</ul>
';
            }
            // Рубрика
            $category = get_the_category();
            if (!empty($category)) {
                // Последняя рубрика в цепочке рубрик
                $last_category = end(array_values($category));
                // Родительськая рубрика
                $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','), ',');
                $cat_parents = explode(',', $get_cat_parents);
                // Перебор родительских категорий и сохранение в переменную $cat_display
                $cat_display = '';
                foreach ($cat_parents as $parents) {
                    $cat_display.= '
<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-cat">' . $parents . '</li>
</ul>
</li>
</ul>';
                    $cat_display.= '
<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator">' . $separator . '</li>
</ul>
</li>
</ul>
';
                }
            }
            // Если это пользовательский тип записи в пользовательской таксономии
            $taxonomy_exists = taxonomy_exists($custom_taxonomy);
            if (empty($last_category) && !empty($custom_taxonomy) && $taxonomy_exists) {
                $taxonomy_terms = get_the_terms($post->ID, $custom_taxonomy);
                $cat_id = $taxonomy_terms[0]->term_id;
                $cat_nicename = $taxonomy_terms[0]->slug;
                $cat_link = get_term_link($taxonomy_terms[0]->term_id, $custom_taxonomy);
                $cat_name = $taxonomy_terms[0]->name;
            }
            // Проверка, если пост находится в рубрике
            if (!empty($last_category)) {
                echo $cat_display;
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>
</ul>
</li>
</ul>
';
                // Иначе, если пост в пользовательской таксономии
 
            } else if (!empty($cat_id)) {
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-cat item-cat-' . $cat_id . ' item-cat-' . $cat_nicename . '"><a class="bread-cat bread-cat-' . $cat_id . ' bread-cat-' . $cat_nicename . '" title="' . $cat_name . '" href="' . $cat_link . '">' . $cat_name . '</a></li>
</ul>
</li>
</ul>
';
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator">' . $separator . '</li>
</ul>
</li>
</ul>
';
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>
</ul>
</li>
</ul>
';
            } else {
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>
</ul>
</li>
</ul>
';
            }
        } else if (is_category()) { // На странице рубрики
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-cat"><strong class="bread-current bread-cat">' . single_cat_title('', false) . '</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_page()) {
            // Стандартная страница
            if ($post->post_parent) {
                // Если у страницы есть родитель
                $anc = get_post_ancestors($post->ID);
                // Получить родителей в правильном порядке
                $anc = array_reverse($anc);
                // Родительский цикл страницы
                if (!isset($parents)) $parents = null;
                foreach ($anc as $ancestor) {
                    $parents.= '
<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-parent item-parent-' . $ancestor . '"><a class="bread-parent bread-parent-' . $ancestor . '" title="' . get_the_title($ancestor) . '" href="' . get_permalink($ancestor) . '">' . get_the_title($ancestor) . '</a></li>
</ul>
</li>
</ul>
';
                    $parents.= '
<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator separator-' . $ancestor . '">' . $separator . '</li>
</ul>
</li>
</ul>
';
                }
                // Отобразить родителей страницы
                echo $parents;
                // Текущая страница
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>
</ul>
</li>
</ul>
';
            } else {
                // Просто отобразить текущую страницу, если нет родителей
                echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>
</ul>
</li>
</ul>
';
            }
        } else if (is_tag()) {
            // Страница метки
            // Получаем информацию из страницы метки
            $term_id = get_query_var('tag_id');
            $taxonomy = 'post_tag';
            $args = 'include=' . $term_id;
            $terms = get_terms($taxonomy, $args);
            $get_term_id = $terms[0]->term_id;
            $get_term_slug = $terms[0]->slug;
            $get_term_name = $terms[0]->name;
            // Отображение имени метки
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-tag-' . $get_term_id . ' item-tag-' . $get_term_slug . '"><strong class="bread-current bread-tag-' . $get_term_id . ' bread-tag-' . $get_term_slug . '">' . $get_term_name . '</strong></li>
</ul>
</li>
</ul>
';
        } elseif (is_day()) {
            // АРХИВ ДНЯ
            // Архив года
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" title="' . get_the_time('Y') . '" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . ' Archives</a></li>
</ul>
</li>
</ul>
';
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator separator-' . get_the_time('Y') . '">' . $separator . '</li>
</ul>
</li>
</ul>
';
            // Архив месяца
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-month item-month-' . get_the_time('m') . '"><a class="bread-month bread-month-' . get_the_time('m') . '" title="' . get_the_time('M') . '" href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('M') . ' Archives</a></li>
</ul>
</li>
</ul>
';
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator separator-' . get_the_time('m') . '">' . $separator . '</li>
</ul>
</li>
</ul>
';
            // Архив дня
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-' . get_the_time('j') . '"><strong class="bread-current bread-' . get_the_time('j') . '"> ' . get_the_time('jS') . ' ' . get_the_time('M') . ' Archives</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_month()) {
            // АРХИВ МЕСЯЦА
            // Архив года
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-year item-year-' . get_the_time('Y') . '"><a class="bread-year bread-year-' . get_the_time('Y') . '" title="' . get_the_time('Y') . '" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . ' Archives</a></li>
</ul>
</li>
</ul>
';
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="separator separator-' . get_the_time('Y') . '">' . $separator . '</li>
</ul>
</li>
</ul>
';
            // Архив месяца
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-month item-month-' . get_the_time('m') . '"><strong class="bread-month bread-month-' . get_the_time('m') . '" title="' . get_the_time('M') . '">' . get_the_time('M') . ' Archives</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_year()) {
            // АРХИВ ГОДА
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-current-' . get_the_time('Y') . '"><strong class="bread-current bread-current-' . get_the_time('Y') . '" title="' . get_the_time('Y') . '">' . get_the_time('Y') . ' Archives</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_author()) {
            // Архив автора
            // Информация об авторе
            global $author;
            $userdata = get_userdata($author);
            // Отобразить имя автора
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-current-' . $userdata->user_nicename . '"><strong class="bread-current bread-current-' . $userdata->user_nicename . '" title="' . $userdata->display_name . '">' . 'Author: ' . $userdata->display_name . '</strong></li>
</ul>
</li>
</ul>
';
        } else if (get_query_var('paged')) {
            // Постстраничный архив
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-current-' . get_query_var('paged') . '"><strong class="bread-current bread-current-' . get_query_var('paged') . '" title="Page ' . get_query_var('paged') . '">' . __('Page') . ' ' . get_query_var('paged') . '</strong></li>
</ul>
</li>
</ul>
';
        } else if (is_search()) {
            // Результаты поиска
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li class="item-current item-current-' . get_search_query() . '"><strong class="bread-current bread-current-' . get_search_query() . '" title="Search results for: ' . get_search_query() . '">Search results for: ' . get_search_query() . '</strong></li>
</ul>
</li>
</ul>
';
        } elseif (is_404()) {
            // Страница 404
            echo '<ul class="' . $breadcrums_class . '">
 	<li style="list-style-type: none;">
<ul class="' . $breadcrums_class . '">
 	<li>' . 'Error 404' . '</li>
</ul>
</li>
</ul>
';
        }
    }
}

Затем отображаем хлебные крошки на вашей странице (например: single.php, page.php, archive.php и т.д.):

1
<?php custom_breadcrumbs(); ?>

Теперь крошки нужно немного приукрасить. Ниже некоторые основные стили CSS, просто используйте следующий код в файле стилей .css:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#breadcrumbs{
    list-style:none;
    margin:10px 0;
    overflow:hidden;
}
 
#breadcrumbs li{
    display:inline-block;
    vertical-align:middle;
    margin-right:15px;
}
 
#breadcrumbs .separator{
    font-size:18px;
    font-weight:100;
    color:#ccc;
}

Если вы новичок без опыта кодирования WordPress, советуют использовать плагин, WordPress SEO имеет большой функционал по навигации.