Wordpress is the most powerful blogging system ever , even you can use it to do any type of sites (E-Commerce , CMS, Galleries .. etc) i think wordpress is going forward to be a framework , all this because of the powerful functions it offers to developers .
I’m going to use some of the wordpress Conditional Tags plus other wordpress functions to create a Breadcrumbs Navigation for wordpress .
Read More about Breadcrumbs and why to use them “Breadcrumb Navigation: Further Investigation of Usage”

Step One
Create a new file within your theme folder and call it breadcrumbs.php or whatever you want
Step Two
In home page we don’t have to show the user the breadcrumbs navigation so we will do this
<?php
if ( is_home() ){}
else {
echo "<div class=\"breadcrumbs\"><ul class=\"breadcrumbs\"> ";
echo "</ul></div>";
}
?>
The code above is pretty simple , we used our first Conditional Tag ( is_home() ) within an if – else statement.
is_home() used to check whether the current page is the homepage or not .
We used it here to do nothing when it’s homepage and applying a simple html codes when it’s not homepage .
Step Three
Let’s Add some css styles .
ul.breadcrumbs {
list-style: none;
padding: 0;
margin: 0;
float: left;
}
ul.breadcrumbs li {
float: left;
margin: 0 5px 0 0;
padding: 0;
}
ul.breadcrumbs li:before {
content: "\00BB \0020";
}
We simply styled the ul.breadcrumbs menu as a horizontal menu and added right angle quote before every “li” member .
Step Four
The blog name must be added before any thing so we’ve to add it to our code .
<?php
if ( is_home() ){}
else {
echo "<div class=\"breadcrumbs\"><ul class=\"breadcrumbs\"> ";
echo "<li><a href=\" ".get_settings('home')." \">";
echo bloginfo('name')."</a></li>";
echo "</ul></div>";
}
?>
In the code above we’ve added the blog name using the wordpress function bloginfo(’name’); within “li” tags and linked it to the blog url using the wordpress function get_settings(’home’) which retrieves the blog link .
Step Five
Output : » Blog Name » Category Name »
Another Output : » Blog Name » Category Name » Sub-Category Name »
We will start our breadcrumbs code in the Category Pages
if ( is_category() ) {
$cate = single_cat_title("",false);
$cat = get_cat_ID($cate);
echo "<li>".(get_category_parents($cat, TRUE," » "))."</li>";
}
In the code above we used is_category() – Conditional Tag , which checks if the current page is a category one or not , then the code above will only be executed within the category pages .
We can simply print the current category using single_cat_title(); but we won’t as we can have multi-leveled categories so we’ve to show the current category plus it’s parents if there any .
We can get the parents of any category using the get_category_parents(); function , and we will use it , but the problem is the output of the single_cat_title(); is the category name and to get the category parents using the get_category_parents(); function we have to pass it the category ID , so we will use get_cat_ID($cate); function to get the current category ID from the current category name .
Note : single_cat_title(”",false); function’s second argument must be false as we don’t want the function to print the category name by itself we want to use the output within another function without printing it so u’ve to declare the second argument to false .
Step Six
Let’s apply our breadcrumbs to archives , search , 404 error pages .
Output 1: » Blog Name » Archives
Output 2: » Blog Name » Search Results
Output 3: » Blog Name » 404 Not Found
elseif ( is_archive() && !is_category() ) {
echo "<li>Archives</li>";
}
elseif ( is_search() ) {
echo "<li>Search Results</li>";
}
elseif ( is_404() ) {
echo "<li>404 Not Found</li>";
}
Obviously you know what’s the function of the above code , it’s pretty easy code .
We first used is_archive() – Conditional Tag to check if the page is a archive pages and printing “Archives” if it’s an Archive page , we also used
&& !is_category() – because category page are also archive pages and we don’t want to print Archives within the category pages so we added && (AND) !(NOT) is_category()
Then we used is_search() – Conditional Tag to check if the page is a search page and printing “Search Results” if it’s a search page .
Then we used is_404() – Conditional Tag to check is the page is a 404 error page and printing “404 Not Found” if it’s a 404 error page .
Step Seven
Let’s apply the breadcrumbs to the single post pages
Output : » Blog Name » Category Name » Post Name
elseif ( is_single() ) {
$category = get_the_category();
$category_id = get_cat_ID($category[0]->cat_name);
$cat_link = get_category_link( $category_id );
echo "<li><a href=\" ". $cat_link ." \">" . $category[0]->cat_name."</a></li>";
echo "<li>";
echo the_title()."</li>";
}
In the code above we used is_single() – Conditional Tag to check if the page is a single post page and printing the category followed by the post name if it’s a single post page.
$category = get_the_category(); To get the category .
$category_id = get_cat_ID($category[0]->cat_name); To get the category ID from the category name ($category[0]->cat_name) , $category[0] that ZERO because we will only get the first category from the categories array (posts may be posted in many categories and we only need the first) .
$cat_link = get_category_link( $category_id ); To get the category URL .
then we just print the category , followed by the post title using the_title()- wordpress function .
Step Eight
Let’s apply the breadcrumbs to the Pages
Output : » Blog Name » Page Name
Another Output : » Blog Name » Page Name » Sub-Page Name
elseif ( is_page() ) {
if ($post->post_parent) {
$children = $post->post_title;
$parent = get_page($post->post_parent);
echo "<li>".$parent->post_title."</li>"."<li>".$children."</li>";
}
elseif ( $post->post_parent == 0 ) {
echo "<li>";
echo the_title()."</li>";
}
}
This is the hard part , as there’s a wordpress function to retrieve page parents . So we wrote some code to retrieve the first page , this will give you the ability to show your breadcrumbs navigation in the second level pages but not in the third level pages as it’ll retrieve the second level page parent only without the first level page parent .
We start our code block with is_page() – Conditional Tag to check if it’s a normal page .
After that we write another if – else statement to check if the current page has a parent and if not we will just print the current page title .
If the current page has a parent :
We store the current page name in the $children variable ( $children = $post->post_title; ) .
Then we use the get_page(); wordpress function to get the parent page and store it in the $parent varialble $parent = get_page($post->post_parent); .
Finally we print the page parent name followed by the page name as shown in line 10 .
elseif ( $post->post_parent == 0 ) {
echo "<li>";
echo the_title()."</li>";
}
The code above means if there’s no page parent just print the title of the page .
The Complete Code
<?php
/*
Wordpress Breadcrumbs code by Banhawi.com
*/
if ( is_home() ){}
else {
echo "<div class=\"breadcrumbs\"><ul class=\"breadcrumbs\"> ";
echo "<li><a href=\" ".get_settings('home')." \">";
echo bloginfo('name')."</a></li>";
if ( is_category() ) {
$cate = single_cat_title("",false);
$cat = get_cat_ID($cate);
echo "<li>".(get_category_parents($cat, TRUE," » "))."</li>";
}
elseif ( is_archive() && !is_category() ) {
echo "<li>Archives</li>";
}
elseif ( is_search() ) {
echo "<li>Search Results</li>";
}
elseif ( is_404() ) {
echo "<li>404 Not Found</li>";
}
elseif ( is_single() ) {
$category = get_the_category();
$category_id = get_cat_ID($category[0]->cat_name);
$cat_link = get_category_link( $category_id );
echo "<li>".(get_category_parents($category, TRUE,""))."</li>";
echo "<li><a href=\" ". $cat_link ." \">" . $category[0]->cat_name."</a></li>";
echo "<li>";
echo the_title()."</li>";
}
elseif ( is_page() ) {
if ($post->post_parent) {
$children = $post->post_title;
$parent = get_page($post->post_parent);
echo "<li>".$parent->post_title."</li>"."<li>".$children."</li>";
}
elseif ( $post->post_parent == 0 ) {
echo "<li>";
echo the_title()."</li>";
}
}
echo "</ul></div>";
}
?>
For search result page , you can display the search query instead of “Search Results” just use this code
elseif ( is_search() ) {
echo "<li>".the_search_query()."</li>";
}
Finally
Just use the include function to include the breadcrumbs script any where in your theme .
<?php include (TEMPLATEPATH . '/breadcrumbs.php'); ?>



Comment by dlv on July 2nd, 2009
awesome tutorial !! i really enjoy it, useful !
thanks Banhawi!
adeux!
Comment by SharePoint developer on July 8th, 2009
very nice article…keep on the good work
Comment by buy_vigrxplus on July 16th, 2009
The best information i have found exactly here. Keep going Thank you
Comment by e on July 28th, 2009
Sorry, I’m new to WordPress; is this how to call the breadcrumbs.php file?
I put this on each template (page, index, archives, etc) in the position where it should be displayed, right?
That seems to work, but I just don’t know if that’s the right/best way to do it.
Comment by e on July 28th, 2009
Sorry again — I don’t know how to make code show here…
[leftangle] ?php include (TEMPLATEPATH . ‘/breadcrumbs.php’); ? [rightangle]
Comment by Banhawi on July 28th, 2009
Thanks all for replies ,
e : yes u’ve to include it in all pages where you want it to be displayed or in the header.php and it’ll be displayed in all pages .
I don’t get your second question , can you clarify the problem ?
Comment by e on July 28th, 2009
My first comment contained the line of code but it didn’t show up. So I posted the line of code without the angle brackets in my second post so you can see it.
Comment by Peter joanson on August 6th, 2009
Amazing post i must say that was worth reading
Comment by mcdonalds free coupons on September 14th, 2009
Thank you very much for that useful blog post.
Comment by Bill Bartmann on September 19th, 2009
This blog rocks! I gotta say, that I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks,
A definite great read..
-Bill-Bartmann
Comment by Nate on October 6th, 2009
Hmm…. it’s leaving out the Parent category on the single post page if the post is in a sub-category, check it out: http://fullforcetraining.com/trainingresource/2009/10/03/awesome-arms-fast/
see how the parent “Old School Workouts” is not in the breadcrumb at the top?
It skips it and goes back to “Your Training Resource”.
Can you fix that? That would be awesome, thanks!
Comment by Nate on October 6th, 2009
Also, what would I need to do to have the search results echo the word(s) the person searched for, instead of just “Search Results” in this section:
elseif ( is_search() ) {
echo “Search Results”;
?
Comment by Joe on November 19th, 2009
Note: if you try to use this code outside the_loop the output of the_title may not be as expected.
Comment by Banhawi on December 11th, 2009
yes you are right .
Comment by Richard on March 12th, 2010
changing line 47 to this make the parent page an active link which it should be:
echo “post_parent).” \”>”.$parent->post_title.” >”.”".$children.”";