Skip to content
PHP

How to add breadcrumb in wordpress without plugin

A
Author
Jun 27, 2020 · 4 min read
How to add breadcrumb in wordpress without plugin

Breadcrumbs are an essential part of website navigation, improving user experience and SEO. They help users understand their location within your site and make it easier to navigate back to previous pages.

Instead of using a plugin, you can create breadcrumbs manually in WordPress by adding a custom function to your theme’s functions.php file. This guide will walk you through how to implement breadcrumbs in WordPress without a plugin.


Why Use Breadcrumbs?

Before diving into the implementation, let’s understand why breadcrumbs are beneficial:

  • Improves User Experience: Users can quickly navigate back to previous sections.
  • Boosts SEO: Search engines like Google use breadcrumbs to understand site structure.
  • Enhances Click-Through Rate (CTR): Breadcrumbs appear in search results, making them more appealing.
  • Reduces Bounce Rate: Users are encouraged to explore other pages instead of leaving.

Step 1: Add Breadcrumb Function in functions.php

To create a custom breadcrumb trail, we need to define a function in the WordPress theme’s functions.php file.

Add the following code to your functions.php file:

function my_breadcrumbs() {
  
  $delimiter = '›'; // Separator between breadcrumb items
  $home = 'Home'; // Label 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;
    $homeLink = get_bloginfo('url');
    echo '<ul class="breadcrumb">';
    echo '<li class="breadcrumb-item"><a href="' . $homeLink . '">' . $home . '</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 . single_cat_title('', false) . $currentAfter;
  
    } elseif (is_single()) {
      $cat = get_the_category();
      $cat = $cat[0];
      echo '<li class="breadcrumb-item"><a href="' . get_category_link($cat) . '">' . $cat->name . '</a></li> ' . $delimiter . ' ';
      echo $currentBefore . get_the_title() . $currentAfter;
  
    } elseif (is_page() && !$post->post_parent) {
      echo $currentBefore . get_the_title() . $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 . get_the_title() . $currentAfter;
  
    } elseif (is_search()) {
      echo $currentBefore . 'Search results for "' . get_search_query() . '"' . $currentAfter;
  
    } elseif (is_404()) {
      echo $currentBefore . 'Error 404' . $currentAfter;
    }
  
    echo '</ul>';
  }
}

Step 2: Call the Breadcrumb Function in header.php

Now that we have defined the breadcrumb function, we need to display it in the correct location on our WordPress site.

Place this code in header.php where you want the breadcrumbs to appear:

<?php if (function_exists('my_breadcrumbs')) my_breadcrumbs(); ?>

This ensures that the breadcrumb navigation appears below the header or wherever you place this code.


Step 3: Style the Breadcrumbs with CSS

To ensure the breadcrumbs match your site’s design, add the following CSS to your theme’s style.css file:

.breadcrumb {
  list-style: none;
  padding: 10px 0;
  background: #f8f9fa;
  border-radius: 5px;
  display: flex;
  gap: 5px;
}

.breadcrumb li {
  display: inline;
  font-size: 14px;
}

.breadcrumb li a {
  text-decoration: none;
  color: #007bff;
}

.breadcrumb li a:hover {
  text-decoration: underline;
}

.breadcrumb-item.active {
  font-weight: bold;
  color: #333;
}

his CSS:

  • Styles the breadcrumb background.
  • Adds spacing between breadcrumb items.
  • Highlights the current page in bold.

Step 4: Test the Breadcrumb Functionality

To ensure everything works correctly, visit different pages on your WordPress site, such as:

  • Homepage: Breadcrumbs should not appear.
  • Category Page: Should show “Home › Category Name.”
  • Single Post: Should show “Home › Category › Post Title.”
  • Page with Parent: Should show “Home › Parent Page › Current Page.”
  • Search Results: Should display “Home › Search results for: keyword.”
  • 404 Page: Should display “Home › Error 404.”

Step 5: Enhance Breadcrumb Functionality (Optional)

To further improve the breadcrumbs, you can:

1. Add Breadcrumbs to Yoast SEO (Without a Plugin)

If you are using Yoast SEO, you can integrate its built-in breadcrumb function:

<?php if ( function_exists('yoast_breadcrumb') ) {
  yoast_breadcrumb('<ul class="breadcrumb">', '</ul>');
} ?>

2. Add Schema Markup for SEO

To make breadcrumbs SEO-friendly, add Schema.org markup:

echo '<ul class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">';

Each breadcrumb item should be wrapped in:

<li itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
  <a itemprop="item" href="your-url">
    <span itemprop="name">Your Page Name</span>
  </a>
  <meta itemprop="position" content="1" />
</li>

This helps Google display breadcrumbs in search results.

Adding breadcrumbs without a plugin in WordPress is an effective way to improve navigation, user experience, and SEO.

A
Author

Author at The Creative Coders.

The monthly build log

One email a month. New posts, lessons, and the occasional strong opinion. No spam.