Open Source Code

WordPress Breadcrumbs

Overview

Author
FWD:labs and the author of wt_is_grandchild()

Requirements
WordPress 4.9+

Version (Last Updated)
1.10 (2017-11-17 09:09:57)

License

The MIT License (MIT)

Copyright (c) 2016 FWD:labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instructions

1. Add this code to your WordPress theme's functions.php file.
2. Add to your template: if (function_exists('fwdlabs_breadcrumbs')) { echo fwdlabs_breadcrumbs(); }

Code

function fwdlabs_breadcrumbs($overrideID = "") {
	
	global $post;
	
	// Start Config
	
	$showHome = false;
	$showCurrent = false;
	$breadcrumbSeparator = "  ›  ";
	$finalSuffix = ""; // Example: " [You Are Here]"
	$breadcrumbWrapperClass = ""; // Example: "gutter"
	
	// End Config

	$breadcrumbID = NULL;
	if (is_numeric($overrideID)) {
		$breadcrumbID = $overrideID;
	} else if (wt_is_grandchild()) {
		$breadcrumbID = get_the_ID(); // for posts
	}

	$displayBreadcrumbs = "";

	$breadcrumbs = array();

	if (is_single() || is_category()) {
		$category = get_the_category($breadcrumbID); 
		if($category[0]){
			if (is_single()) {
				$current_title .= '<a href="'.get_category_link($category[0]->term_id ).'">';
			}
			if (is_single() || $showCurrent == true) {
				$current_title .= $category[0]->cat_name;
			}
			if (is_single()) {
				$current_title .= '</a>';
			} else {
				$current_title .= $finalSuffix;
			}
			
			if ($current_title <> "") {
				$breadcrumbs[] = $current_title;
			}
			
		}
	}
	
	if (is_search() && !is_numeric($overrideID)) {
		
		if ($showCurrent == true) {
			$current_title .= "Search".$finalSuffix;
			$breadcrumbs[] = $current_title;
		}
				
	} else {

		if($post->post_parent) {
			if (wt_is_grandchild() && !is_numeric($breadcrumbID)) {
				// $yourid = get_the_ID();
				$parent = get_post_ancestors();
				$grandparent = $parent[1];
				// echo "<pre>"; print_r($parent); echo "</pre>";
				$grandparent_title = get_the_title($grandparent);
				$grandparent_permalink = get_permalink($grandparent);
				$breadcrumbs[] = '<a href="'.$grandparent_permalink.'">'.$grandparent_title.'</a>';
			} else if (is_numeric($breadcrumbID)) {
				// $yourid = get_the_ID();
				$parent = get_post_ancestors($breadcrumbID);
				$grandparent = $parent[1];
				// echo "<pre>"; print_r($parent); echo "</pre>";
				$grandparent_title = get_the_title($grandparent);
				$grandparent_permalink = get_permalink($grandparent);
				$breadcrumbs[] = '<a href="'.$grandparent_permalink.'">'.$grandparent_title.'</a>';
			} else {
				$parent_title = get_the_title($post->post_parent);
				$parent_permalink = get_permalink($post->post_parent);
				$breadcrumbs[] = '<a href="'.$parent_permalink.'">'.$parent_title.'</a>';
			 }
		}
		
		if (wt_is_grandchild() && !is_numeric($breadcrumbID)) {
    		$parent_title = get_the_title($post->post_parent);
    		$parent_permalink = get_permalink($post->post_parent);
			$breadcrumbs[] = '<a href="'.$parent_permalink.'">'.$parent_title.'</a>';
		} else if (is_numeric($breadcrumbID)) {
			$parent_ancestors = get_post_ancestors($breadcrumbID);
			$parent = $parent_ancestors[0];
    		$parent_title = get_the_title($parent);
    		$parent_permalink = get_permalink($parent);
    		if ($breadcrumbID <> $parent) {
				$breadcrumbs[] = '<a href="'.$parent_permalink.'">'.$parent_title.'</a>';
    		}
		}

	}

	if (is_single() || is_page()) {
   		$current_title = get_the_title($post->ID);
		if (!is_numeric($overrideID)) {
	   		$current_title = get_the_title($overrideID);		
		}
		$current_title .= $finalSuffix;
		if ((is_front_page() && $showHome == false)) {
			// Skip because of config
		} else {
			if ($showCurrent == true) {
				$breadcrumbs[] = $current_title;
			}
		}
	}
				
	$countBreadcrumbs = count($breadcrumbs);
	if ($countBreadcrumbs > 0) {
	
		if ($breadcrumbWrapperClass <> "") {
		$displayBreadcrumbs .= '<div class="gutter">';
		}
		
		$displayBreadcrumbs .= '<small class="first breadcrumb">';
					
		$get_site_url = get_site_url();
					
		if (!is_numeric($overrideID)) {
			if ($showHome == true) {
				$displayBreadcrumbs .= '<a href="'.$get_site_url.'/">Home</a>'.$breadcrumbSeparator;
			}
		} else {
			$displayBreadcrumbs .= 'Found under ';
		}
		
		$i = 1;
		foreach ($breadcrumbs as $breadcrumb) {
			$displayBreadcrumbs .= "\n".$breadcrumb;
			if ($i < $countBreadcrumbs) {
				$displayBreadcrumbs .= $breadcrumbSeparator;
			}
			$i++;
		}
		$displayBreadcrumbs .= "\n".'</small>';
		
		if ($breadcrumbWrapperClass <> "") {
		$displayBreadcrumbs .= '</div><!-- /gutter -->'."\n";
		}
		
	}
				
	return $displayBreadcrumbs;

}

// Get Depth
// Written by web-templates.nu
// Source: http://www.web-templates.nu/2008/09/07/get-depth-like-is_child-is_grandchild/
if (!function_exists('wt_get_depth')) {
	function wt_get_depth($id = '', $depth = '', $i = 0) {
	global $wpdb;
	global $post;

	if($depth == '')
	{
		if(is_page())
		{
			if($id == '')
			{
				$id = $post->ID;
			}
			$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'");
			return wt_get_depth($id, $depth, $i);
		}
	}
	elseif($depth == "0")
	{
		return $i;
	}
	else
	{
		$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'");
		$i++;
		return wt_get_depth($id, $depth, $i);
	}
}
}
// Grandchild Page
if (!function_exists('wt_is_grandchild')) {
	function wt_is_grandchild($id = '') {
		if(wt_get_depth($id)>1)
		{
			return true;
		}
	}
}

Changelog

1.1: Added configurations for showing home as initial breadcrumb, current page as final breadcrumb, separator between breadcrumbs (if any), final suffix (e.g. "you are here"), div wrapper class (if any), and no markup if no breadcrumbs present after configured.
1.0: Initial release.

Feedback

Questions, comments, concerns or ideas about this? Please let us know.

Twitter   Facebook   Instagram