we need to show breadcrumbs in wordpress site. For pages and category we can create breadcrumbs in wordpress. Shown, How to create breadcrumbs in wordpress.
How to create breadcrumbs in wordpress
We need to put following code in functions.php file.
01
<!--?<span
class
=
"hiddenSpellError"
pre=
""
data-mce-bogus=
"1"
-->php
02
function
wordpress_breadcrumbs() {
04
$delimiter
=
'»'
;
06
$currentBefore
=
'<span class="current">'
;</span>
09
if
( !is_home() && !is_front_page() || is_paged() ) {
11
echo
'<div id="crumbs">'
;
14
$home
= get_bloginfo(
'url'
);
15
echo
'<a href="'
.
$home
.
'">'
.
$name
.
'</a> '
.
$delimiter
.
' '
;
17
if
( is_category() ) {
19
$cat_obj
=
$wp_query
->get_queried_object();
20
$thisCat
=
$cat_obj
->term_id;
21
$thisCat
= get_category(
$thisCat
);
22
$parentCat
= get_category(
$thisCat
->parent);
23
if
(
$thisCat
->parent != 0)
echo
(get_category_parents(
$parentCat
, TRUE,
' '
.
$delimiter
.
' '
));
24
echo
$currentBefore
.
'Archive by category '
';
26
echo
''
' .
$currentAfter
;
28
}
elseif
( is_day() ) {
29
echo
'<a href="'
. get_year_link(get_the_time(
'Y'
)) .
'">'
. get_the_time(
'Y'
) .
'</a> '
.
$delimiter
.
' '
;
30
echo
'<a href="'
. get_month_link(get_the_time(
'Y'
),get_the_time(
'm'
)) .
'">'
. get_the_time(
'F'
) .
'</a> '
.
$delimiter
.
' '
;
31
echo
$currentBefore
. get_the_time(
'd'
) .
$currentAfter
;
33
}
elseif
( is_month() ) {
34
echo
'<a href="'
. get_year_link(get_the_time(
'Y'
)) .
'">'
. get_the_time(
'Y'
) .
'</a> '
.
$delimiter
.
' '
;
35
echo
$currentBefore
. get_the_time(
'F'
) .
$currentAfter
;
37
}
elseif
( is_year() ) {
38
echo
$currentBefore
. get_the_time(
'Y'
) .
$currentAfter
;
40
}
elseif
( is_single() ) {
41
$cat
= get_the_category();
$cat
=
$cat
[0];
42
echo
get_category_parents(
$cat
, TRUE,
' '
.
$delimiter
.
' '
);
47
}
elseif
( is_page() && !
$post
->post_parent ) {
52
}
elseif
( is_page() &&
$post
->post_parent ) {
53
$parent_id
=
$post
->post_parent;
54
$breadcrumbs
=
array
();
56
$page
= get_page(
$parent_id
);
57
$breadcrumbs
[] =
'<a href="'
. get_permalink(
$page
->ID) .
'">'
. get_the_title(
$page
->ID) .
'</a>'
;
58
$parent_id
=
$page
->post_parent;
60
$breadcrumbs
=
array_reverse
(
$breadcrumbs
);
61
foreach
(
$breadcrumbs
as
$crumb
)
echo
$crumb
.
' '
.
$delimiter
.
' '
;
66
}
elseif
( is_search() ) {
67
echo
$currentBefore
.
'Search results for '
' . get_search_query() . '
''
.
$currentAfter
;
69
}
elseif
( is_tag() ) {
70
echo
$currentBefore
.
'Posts tagged '
';
72
echo
''
' .
$currentAfter
;
74
}
elseif
( is_author() ) {
76
$userdata
= get_userdata(
$author
);
77
echo
$currentBefore
.
'Articles posted by '
.
$userdata
->display_name .
$currentAfter
;
79
}
elseif
( is_404() ) {
80
echo
$currentBefore
.
'Error 404'
.
$currentAfter
;
83
if
( get_query_var(
'paged'
) ) {
84
if
( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo
' ('
;
85
echo
__(
'Page'
) .
' '
. get_query_var(
'paged'
);
86
if
( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo
')'
;
After that put following code in header.php file.
1
<?php
if
(function_exists(
'wordpress_breadcrumbs'
)) wordpress_breadcrumbs(); ?>
After putting above code we can see the breadcrumbs as following;
Home » Parent Page » Sub Page1 » Sub Page2
Home » Category » Subcategory » Post Name
How to create breadcrumbs in wordpress
Code has been taken from above Source
good information about creating breadcrumbs