How to add breadcrumbs to WordPress theme


In this tutorial I will explain how to add custom breadcrumbs WordPress theme. For testing purposes, we will be using the theme we previously developed. 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

Custom breadcrumbs to WordPress – development

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.

Custom breadcrumbs modification

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.

Summary
Custom breadcrumbs - how to add breadcrumbs to wordpress theme
Article Name
Custom breadcrumbs - how to add breadcrumbs to wordpress theme
Description
How to add custom breadcrumbs to wordpress theme. Development tutorials with code examples and detailed explanation. Main loop modification and customisation. Custom style and adjustments.
Author
Publisher Name
wppotion


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. node js wordpress, client.getPostType( 'title to search'); returns TypeError: fn is not a function in node_moduleslibwordpress.js:207:4
  2. Fixing Mismatched Image Class in Existing Posts
  3. How to make an exception in my PHP code to exempt a certain URL from being redirected?
  4. How to wordpress pagination url change
  5. Wordpress website's navbar dropdown is not working
  6. AJAX in WordPress Template Not Working in WordPress Plugin Class
  7. Insert jquery var into php encode to pull ACF fields for Wordpress template
  8. An error occurred after updating Wordpress 5.3
  9. Get site URL in Wordpress
  10. Does Google Form on AMP require HTTPS?

WordPress problems we are working on

  1. Wordpress WPLMS add attachment field in edit course
  2. Is the WordPress $wpdb object secure for making SQL calls with PHI information?
  3. WordPress custom related posts using Ad Inserter
  4. Redirect to URL based on product purchased in Wordpress
  5. React-router-dom problem with routes after creating app with react-create-wptheme
  6. Re-Building Website to Wordpress with Full-Width Gallery Transitions (animsition + owl-carousel)
  7. How to get Session of word press from other site If some user is currently active in word-press
  8. I can't get wordpress custom footer to work on about us page
  9. get_post_meta() not display field value for first item in wordpress loop
  10. Custom Wordpress Development - building a custom page builder

Resolved issues

  1. MySql Connection in Anaconda Jupyter Notebook
  2. How to add Custom Fields of Custom Post to REST API?
  3. WordPress - Override template image resizing
  4. How to integrate 3rd party api / json in wordpress
  5. ACF Group Sub Field Not Displaying
  6. What will ES5 code look like for Wordpress CheckboxControl
  7. Wordpress forum in Spanish
  8. wordpress theme footer.php file have some malicious code
  9. Setup 301 redirect for .html pages Wordpress
  10. Value is pulled wordpress
wppotion - powered by persistence and passion