Wordpress Breadcrumbs

19th December 2009 - 7 minutes read time

I have been playing about with breadcrumb trails in Wordpress recently so I thought I would post the code here in case anyone else finds it useful. The basic idea is to try to find out where the current page is situated within the site. This means checking for pages and categories with their parents. Once we get down to the post level we need to try some things in order to find out where the current post is within the site.

Once the trail array has been filled with items it is looped through and converted into a string.

To use this function put the following code into your functions.php file in your template.

  1. <?php
  2. /**
  3.  * Echo or return a formatted list of breadcrumbs.
  4.  *
  5.  * @param array $args An array of arguments to controll the output of the
  6.  * function.
  7.  * @return string The breadcrumb list.
  8.  */
  9. function get_breadcrumbs($args = array())
  10. {
  11. global $post;
  12.  
  13. if (is_string($args)) {
  14. parse_str($args, $args);
  15. }
  16.  
  17. // Set up defaults.
  18. $defaults = array(
  19. 'separator' => ' &gt; ',
  20. 'linkFinal' => false,
  21. 'echo' => true,
  22. 'printOnHome' => true,
  23. 'before' => '',
  24. 'after' => '',
  25. );
  26.  
  27. // Merge the defaults with the parameters.
  28. $options = array_merge($defaults, (array)$args);
  29.  
  30. // Initialise the trail with the current post.
  31. $trail = array($post);
  32.  
  33. // Initialise the output.
  34. $output = '';
  35.  
  36. $currentCategory = 0;
  37.  
  38. if (is_front_page() == true && $options['printOnHome'] == false) {
  39. /**
  40.   * If this is the front page and the option to prevent priting on the
  41.   * home page is disabled then echo or return the empty string depending
  42.   * on the echo option.
  43.   */
  44. if ($options['echo'] == true) {
  45. echo $output;
  46. }
  47. return $output;
  48. }
  49.  
  50. if (is_page()) {
  51. // If the current page is a page.
  52. $parent = $post;
  53. while ($parent->post_parent) {
  54. $parent = get_post($parent->post_parent);
  55. array_unshift($trail, $parent);
  56. }
  57. } elseif (is_category()) {
  58. // The current page is a category page.
  59. $trail = array();
  60. $currentCategory = get_query_var('cat');
  61. $category = get_category($currentCategory);
  62. while ($category->category_parent > 0) {
  63. array_unshift($trail, $category);
  64. $category = get_category($category->category_parent);
  65. }
  66. // Add the final category to the trail.
  67. array_unshift($trail, $category);
  68. } else {
  69. // The current page will be a post or set of posts.
  70. $path = explode('/', $_SERVER['REQUEST_URI']);
  71. $path = array_filter($path);
  72. while (count($path) > 0) {
  73. $page = get_page_by_path(implode('/', $path));
  74. if ($page != NULL) {
  75. array_unshift($trail, $page);
  76. }
  77. array_pop($path);
  78. }
  79.  
  80. if (count($trail) == 1) {
  81. // No pages found in path, try using categories.
  82. $category = get_the_category();
  83. $category = $category[0];
  84. while ($category->category_parent > 0) {
  85. array_unshift($trail, $category);
  86. $category = get_category($category->category_parent);
  87. }
  88. array_unshift($trail, $category);
  89. }
  90. }
  91.  
  92. $show_on_front = get_option('show_on_front');
  93. if ('posts' == $show_on_front) {
  94. // The home page is a list of posts so just call it Home.
  95. $output .= '<li class="breadcrumb-item" id="breadcrumb-0"><a href="' . get_bloginfo('home') . '" title="Home">Home</a></li>' . "\n"; // home page link
  96. } else {
  97. // Otherwise the front page is a page so get the page name.
  98. $page_on_front = get_option('page_on_front');
  99. $home_page = get_post($page_on_front);
  100. $output .= '<li class="breadcrumb-item" id="breadcrumb-' . $home_page->ID . '"><a href="' . get_bloginfo('home') . '" title="' . $home_page->post_title . '">' . $home_page->post_title . '</a></li>' . "\n"; // home page link
  101. if ($trail[0]->ID == $page_on_front) {
  102. array_shift($trail);
  103. }
  104. }
  105.  
  106. if (is_front_page() == false) {
  107. // If we aren't on the home page then construct the output.
  108. foreach ($trail as $key => $page) {
  109. // Every item in the trail will be either a post/page object or a category.
  110. if (count($trail) - 1 == $key && $options['linkFinal'] == false) {
  111. // If we are on the last page and the option to link the final link is true.
  112. if (isset($page->post_title)) {
  113. $output .= '<li class="breadcrumb-item" id="breadcrumb-' . $page->ID . '">' . $options['separator'] . ' ' . $page->post_title . '</li>' . "\n";
  114. } elseif (isset($page->cat_name)) {
  115. $output .= '<li class="breadcrumb-item" id="breadcrumb-cat-' . $page->term_id . '">' . $options['separator'] . ' ' . $page->cat_name . '</li>' . "\n";
  116. }
  117. } else {
  118. // Create the link to the page or category
  119. if (isset($page->post_title)) {
  120. $output .= '<li class="breadcrumb-item" id="breadcrumb-' . $page->ID . '">' . $options['separator'] . '<a href="' . get_page_link($page->ID) . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>' . "\n";
  121. } elseif (isset($page->cat_name)) {
  122. $output .= '<li class="breadcrumb-item" id="breadcrumb-cat-' . $page->term_id . '">' . $options['separator'] . '<a href="' . get_category_link($page->term_id) . '" title="' . $page->cat_name . '">' . $page->cat_name . '</a></li>' . "\n";
  123. }
  124. }
  125. }
  126. }
  127.  
  128. // Finish off the html of the ul
  129. $output = "<ul>\n" . $output . "</ul>\n";
  130.  
  131. // Add other elements
  132. $output = $options['before'] . $output . $options['after'];
  133.  
  134. if ($options['echo'] == true) {
  135. // Print out the $output variable.
  136. echo $output;
  137. }
  138. // Return
  139. return $output;
  140. }

Print out the breadcrumb trail on your page like this:

Several options are also available to change the way the function works. These are as follows:

  • separator: The string to separate the items in the breadcrumb trail, defaults to >.
  • linkFinal: Whether or not to print the last link as a link. Defaults to false.
  • echo: Whether or not to print out the breadcrumb in the function. Defaults to true.
  • printOnHome: Print the breadcrumb on the home page. Defaults to true.

You can set the options for the get_breadcrumbs() function in much the same way as normal Wordpress functions. This can be through a parameter string or a array like this.

  1. <?php
  2. get_breadcrumbs(
  3. array(
  4. 'separator' => ' &gt; ',
  5. 'linkFinal' => false,
  6. 'echo' => true,
  7. 'printOnHome' => false,
  8. )
  9. );
  10. ?>

This will print out something like the following:

  1. <ul>
  2. <li class="breadcrumb-item" id="breadcrumb-0"><a href="http://www.example.com/wordpress_test" title="Home">Home</a></li>
  3. <li class="breadcrumb-item" id="breadcrumb-174"> &gt; <a href="http://www.example.com/wordpress_test/level-1" title="Level 1">Level 1</a></li>
  4. <li class="breadcrumb-item" id="breadcrumb-173"> &gt; <a href="http://www.example.com/wordpress_test/level-1/level-2" title="Level 2">Level 2</a></li>
  5. <li class="breadcrumb-item" id="breadcrumb-172"> &gt; Level 3</li>
  6. </ul>

Because Wordpress is one of those applications that can be set up in a number of different ways I have tried to account for as many situations as I can. However, if you try this breadcrumb function out and it doesn't work as expected post a comment here and let me know.

UPDATE: Added before and after options to the breadcrumb output string.

Comments

Permalink
While looking for php isset on Sunday I came across your blog and post about Wordpress Breadcrumbs | #! code. I could not believe the amount of quality material that exists on this site. The site is extremely eye catching and pulls the reader straight it, the articles are great quality and are very professionally written. I have seen too many of these sites where it looks like they pay an 8 year old to do the writing - Not this one. Your site is easily the best that I have seen in a long while.

Php Isset (Sun, 03/21/2010 - 19:31)

Add new comment

The content of this field is kept private and will not be shown publicly.