How to add breadcrumb in wordpress without plugin
Adding breadcrumbs to a WordPress website without using a plugin can be done by creating a custom function and adding it to your theme’s functions.php
file. Here’s an example of how to do this:
- Open your theme’s
functions.php
file and add the following code:
function my_breadcrumbs() {
$delimiter = '';
$name = 'Home'; //text for the 'Home' link
$currentBefore = '<li class="breadcrumb-item active" aria-current="page">';
$currentAfter = '</li>';
if ( !is_home() && !is_front_page() || is_paged() ) {
global $post;
$home = get_bloginfo('url');
echo '<li class="breadcrumb-item"><a href="' . $home . '">' . $name . '</a></li> ' . $delimiter . ' ';
if ( is_category() ) {
global $wp_query;
$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, ' ' . $delimiter . ' '));
echo $currentBefore . 'Archive by category ';
single_cat_title();
echo '' . $currentAfter;
} elseif ( is_day() ) {
echo '<li class="breadcrumb-item"><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li> ' . $delimiter . ' ';
echo '<li class="breadcrumb-item"><a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a></li> ' . $delimiter . ' ';
echo $currentBefore . get_the_time('d') . $currentAfter;
} elseif ( is_month() ) {
echo '<li class="breadcrumb-item"><a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a></li> ' . $delimiter . ' ';
echo $currentBefore . get_the_time('F') . $currentAfter;
} elseif ( is_year() ) {
echo $currentBefore . get_the_time('Y') . $currentAfter;
} elseif ( is_single() && !is_attachment() ) {
$cat = get_the_category(); $cat = $cat[0];
echo '<li class="breadcrumb-item"><a href=""'.get_category_parents($cat, TRUE, ' ' . $delimiter . ' '). '</a></li> ';
echo $currentBefore;
echo get_the_title();
echo $currentAfter;
} elseif ( is_attachment() ) {
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
echo '<li class="breadcrumb-item"><a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> </li>' . $delimiter . ' ';
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_page() && !$post->post_parent ) {
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_page() && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = '<li class="breadcrumb-item"><a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a></li>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
echo $currentBefore;
the_title();
echo $currentAfter;
} elseif ( is_search() ) {
echo $currentBefore . 'Search results for ' . get_search_query() . '' . $currentAfter;
} elseif ( is_tag() ) {
echo $currentBefore . 'Posts tagged ';
single_tag_title();
echo '' . $currentAfter;
} elseif ( is_author() ) {
global $author;
$userdata = get_userdata($author);
echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
} elseif ( is_404() ) {
echo $currentBefore . 'Error 404' . $currentAfter;
}
if ( get_query_var('paged') ) {
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
echo __('Page') . ' ' . get_query_var('paged');
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
}
}
}
This function uses WordPress conditionals to determine what type of page is being displayed and generates the breadcrumb trail accordingly.
- Add the following code to your theme’s header.php file where you want the breadcrumbs to appear:
<?php my_breadcrumbs() ?>
This code checks to see if the current page is the front page (home page) and only displays the breadcrumbs if it is not.
- Customize the HTML and CSS as needed to match the design of your theme.
With these steps, you should have a working breadcrumb trail that is generated without using a plugin. You can also modify the function to add additional conditionals or change the order of the breadcrumbs.