How to add breadcrumbs to WordPress theme


In this tutorial I will explain how to add breadcrumbs to the custom WordPress theme we previously created. This is actually a continuation of the previous tutorial How to create a WordPress theme from scratch. The breadcrumbs feature is quite useful when it comes to faster navigation and especially SEO. When you have it enabled, you may easily switch the pages, which makes them better linked and the search engine crowlers will be able to index your content faster.

Here is screen shot of the final result:

How to add breadcrumbs to WordPress theme

How to add breadcrumbs to WordPress theme

To explain better the breadcrumbs feature I have added a few categories and posts. All of them contain simple text. I have also created a new file in the root folder of the test theme. Its name is functions.php and at the moment it has only one PHP opening tag:

function the_breadcrumb() {

$separator = ” » “;

if (!is_home()) {

echo ‘’;

bloginfo(‘name’);

echo “ “.$separator;

if (is_category() || is_single()) {

the_category(‘title_li=’);

if (is_single()) {

echo $separator;

the_title();

}

} elseif (is_page()) {

echo the_title();

}

}

}

The idea of this function is to obtain the home URL , the links to the categories or posts. The structure depends on where on the site you are at the moment. If you click on a category page the breadcrumb structure will be similar to :

My Blog » Category 2

Where my blog is the home URL , the arrows are the separator and category 2 is the name of the category. The same way if you visit a post page the structure will be changed to :

My Blog » Category 2 » Where can I get some?

In this case “Where can I get some?” Is the title of my post. If you do not like the separator you may easily change it by updating the string in the double quotes:

$separator = " » ";

You may add dashes, dots or whatever you want.

By default the custom breadcrumbs come as links which are underlined. To change that and have underlines only upon hover you may add these two lines in your style.css file :

.breadcrumbs a {text-decoration: none;}
 .breadcrumbs a:hover {text-decoration: underline;}

I have also adjusted a bit the main loop I previously added to the theme. The exact line I changed is this one:

<h1><?php the_title(); ?></h1>

Now the line is as follows:

<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>

The idea behind this was to make the page title link to the article itself. This way if the article is too long and there is no read more button, you can click the title and review the entire article or post.

So far we updated the main loop and created a function. Now it is time to call that function and check the result. To do so edit header.php file and replace the following line:

with :

<div class="breadcrumbs rounded"><?php the_breadcrumb(); ?></div>

When done, save your file and check the website. During the page generation WordPress will reach our custom function and check the default ones, then it will check our functions.php file and based on the code it finds there, you will see the breadcrumbs on your page.


Do you want to share your opinion?

Your email address will not be published. Required fields are marked *


*

We are not industry specific. We are WordPress specific. We work with everyone to help them find solutions with their troubles.
If you show us the problem you are experiencing, we will show you how to fix it. It is that simple.


Reported WordPress issues

  1. Single Product page loses current class in parent menu page?
  2. Open Graph scraping www URL instead the URL it's given
  3. How to build for Single / Recurring events metabox for custom post type of events in wordpress?
  4. how to replace strings in the values of a particular custom field in all posts?
  5. a good and cheap server that you can change max execution time to 180 [on hold]
  6. jQuery scroll no run
  7. Wordpress template shown after migration to new web host
  8. wp_nav_menu not applying the right menu
  9. How can I dynamically call a Custom Value and insert it into a $ Variable? Current effort breaks website
  10. Posts Table with Search and Sort WordPress Plugin

WordPress problems we are working on

  1. Changing order of a list in FormidablePro with jQuery UI
  2. How can I insert / upload an external html file to wordpress?
  3. Wordpress add_post_meta with increment
  4. Display -webkit-flex not working in Ipad (chrome and safari)
  5. No products were found matching your selection
  6. How to remove and animation on menu for a Wordpress website using Divi theme
  7. How to insert an invideo ad script into an iframed video served from google drive
  8. How to start/play a wordpress acf oEmbed video with a seperate button?
  9. How to update a WordPress page from Java (org.springframework.web.client.RestClientException)?
  10. Best htaccess practises for Wordpress which compatible with Cloudflare and Flexible SSL?

Resolved issues

  1. upload media to wordpress rest api
  2. How to centre menu of Wordpress website without effecting mobile menu
  3. How can I call a value from a Custom Text Field and dynamically place it in Form Attribute?
  4. Center items and align text left on mobiles using flex?
  5. Reduce the gap or white space between the title and 'hr' line using HTML
  6. Wordpress Hide Add to Cart Multiple Categories
  7. What's wrong with this PHP code? It's for a WordPress forum plugin
  8. php array - groups of 3
  9. Wordpress redirect within plugin files
  10. Specific .gif getting corrupt in wordpress
wppotion - powered by persistence and passion