How add breadcrumb in WordPress Custom Theme via functions.php

Breadcrumb

Add breadcrumb and show post’s or page’s location in your WordPress website. So there is two deferent code to show Breadcrumb.

How To Add Breadcrumb using code

  • Login cpanel.
  • Add given code in functions.php of active theme’s folder.

Use this code when want to show only category name 

function get_breadcrumb() {
echo '<a href="'.home_url().'" rel="nofollow">Home</a>';
if (is_category() || is_single()) {
echo "&nbsp;&#187;&nbsp;";
the_category(' &bull; ');
if (is_single()) {
echo " ";
}
} elseif (is_page()) {
echo "&nbsp;&#187;&nbsp;";
echo the_ID();
} elseif (is_search()) {
echo "&nbsp;&#187;&nbsp;Search Results for... ";
echo '"<em>';
echo the_search_query();
echo '</em>"';
}
}
  • Then paste <?php get_breadcrumb(); ?> where you want to show Breadcrumb.

Use this code when want to show only category with parent category name 

<?php
if (!is_home()) {
echo '<div class="thekroyaard_breadcrumbs">';
echo '<a href="' . esc_url(home_url('/')) . '">Home</a>';
if (is_category() || is_single()) {
echo '<span class="separator"> / </span>';
$categories = get_the_category();
if ($categories) {
$category_path = '';
$category_parents = array_reverse(get_ancestors($categories[0]->term_id, 'category'));
foreach ($category_parents as $category_parent) {
$category_path .= '<a href="' . esc_url(get_category_link($category_parent)) . '">' . esc_html(get_cat_name($category_parent)) . '</a>';
$category_path .= '<span class="separator"> / </span>';
}
echo $category_path;
}
the_category(' <span class="separator">/</span> ');
if (is_single()) {
echo ' <span class="separator">/</span> ';
the_title();
}
} elseif (is_page()) {
echo ' <span class="separator">/</span> ';
echo the_title();
}
echo '</div>';
}
?>
  • Then paste this code where you want to show Breadcrumb.

When you complete process, Then SAVE your file.