Kriesi.at – new media design

Wordpress Tutorial: Simple Breadcrumb Navigation

I am currently working on some larger projects, and some of them are in desperate need of a breadcrumb navigation, since there are so many sub categories and pages, that users often have a hard time not to lose track of their current position on the site.

After searching the web for an adequate plugin, the only Breadcrumb navigation I could find was Breadcrumb NavXT. The plugin is basically the only one which supports nested pages as well as nested categories. It has a ton of options… to my mind there are way to many. After testing the plugin on a blank installation my Database query count went from 17 to 59 even if I was on the starting page where no breadcrumb navigation was displayed. So I decided to code a lightweight version for myself.

What you will learn here, are the basics of creating a simple breadcrumb navigation. This tutorial doesn't offer a complete script, just some snippets to experiment with =)

First of all here is the whole script:

function my_breadcrumb() {
         if ( !is_front_page() ) {
		echo '<p class="breadcrumb"><span class="breadcrumb_info">You are here:</span> <a href="';
		echo get_option('home');
		echo '">';
		bloginfo('name');
		echo "</a> &raquo; ";
		}

		if ( is_category() || is_single() ) {
			$category = get_the_category();
			$ID = $category[0]->cat_ID;
			echo get_category_parents($ID, TRUE, ' &raquo; ', FALSE );
		}

		if(is_single() || is_page()) {the_title();}
		if(is_tag()){ echo "Tag: ".single_tag_title('',FALSE); }
		if(is_404()){ echo "404 - Page not Found"; }
		if(is_search()){ echo "Search"; }
		if(is_year()){ echo get_the_time('Y'); }			

		echo "</p>";

          }

I am using Wordpress conditional Tags to check which section/post or page we are currently viewing. As you can see at the top, I don't want to display the breadcrumb on the starting page. If we aren't viewing the starting page, the first level – a link to the starting page – will be displayed.

if ( is_category() || is_single() ) {
	        $category = get_the_category();
		$ID = $category[0]->cat_ID;
		echo get_category_parents($ID, TRUE, ' &raquo; ', FALSE );
		}

This is the trickiest part of the whole script since it uses some uncommon WordPress functions. It basically asks WordPress to display all parent categories of the current category/post. This way we can handle nested categories as well

If you want more information on get_the_category and get_category_parents just check the Wordpress Documentation =)

if(is_single() || is_page()) {the_title();}
if(is_tag()){ echo "Tag: ".single_tag_title('',FALSE); }
if(is_404()){ echo "404 - Page not Found"; }
if(is_search()){ echo "Search"; }
if(is_year()){ echo get_the_time('Y'); }

This one is pretty straight forward: We are using conditional comments again to handle some of the various scenarios like post, pages, tag and year archives, 404 sites etc.

If you want to create a script that can handle all possible scenarios you should check the conditional tags reference to see which cases need attention.

These short snippets should invite you to experiment a little bit since they can build the foundation to a complete lightweight breadcrumb script. It is far from complete now (nested pages don't work, nested categories need a few tweaks, attachment handling is missing etc) but I guess its enough to start with ;)
I am currently scripting a complete Breadcumb Navigation for my own and I am pretty satisfied with the outcome. It only needs some more minor tweaks to be ready for the public, but I dont't know if I can spare some time in the upcoming future so I wanted to share these basics now, just in case you need them =)

What you can expect when my script is complete: a very lightweight breadcrumb script with little to no options but the possibilty to tweak it easily with little php knowledge, and the power to display a breadcrumb navigation without stressing your database (The NavXT-plugin uses about 40 queries, mine will use about 0-4)

Thanks to Frank Bültge for the inspiration ;)

Responses to “Wordpress Tutorial: Simple Breadcrumb Navigation”

Trackbacks

  1. WP Limits » Blog Archive » Simple Breadcrumb Navigation
  2. Technogeeks» Blog Archive » Wordpress Tutorial: Simple Breadcrumb Navigation
  3. Create lightweight breadcrumb navigation (hack)
  4. 250 Wordpress Tutorials
  5. links for 2009-05-03 » Von admin » Beitrag » von pixeln und paddeln
  6. 36 New Wordpress Tips, Tricks, Tutorials & Hacks | TechnoBuzz.net
  7. 36 New Wordpress Tips, Tricks, Tutorials & Hacks | TechnoBuzz.net

Leave a Reply