WordPress Breadcrumbs – Jankified

So this may or may not be a good way to do this, but it works. Good enough for the quick overview. If you’ve got fixes for me, feel free to shout ’em out.

The result you see comes from several different web pages mashed together into this Frankenstein of a snippet, but I couldn’t find anyone that went so far back and therefore wanted to share with all of you good people.

		// grab current slug
		$post_name = $post->post_name;
		// grab parent page based off of current
		$parent = $post->post_parent;
		// get the page information of $parent
		$parent_link = get_permalink($parent);
		$parent_name = get_page($parent);
		$parent_echo = '<a href="' . $parent_link . '">' . get_the_title($parent) . '</a> > ';
		// get slug of $parent
		$parent_slug = $parent_name->post_name;

		$grandparent = $parent_name->post_parent;
		if ($grandparent) {
			$grandparent_link = get_permalink($grandparent); 
			$grandparent_name = get_page($grandparent);
			$grandparent_slug = $grandparent_name->post_name;
			$gp = '<a href="' . $grandparent_link.'">' . get_the_title($grandparent) . '</a> > ';

		// bring on the great grandparents
		$great_grandparent = $grandparent_name->post_parent;
		if($great_grandparent) {
			$great_grandparent_link = get_permalink($great_grandparent); 
			$great_grandparent_name = get_page($great_grandparent);
			$great_grandparent_slug = $great_grandparent_name->post_name;
			$ggp = '<a href="'.$great_grandparent_link.'">' . get_the_title($great_grandparent) . '</a> > ';

		// if has parent, run foreach
		if( $parent_slug != $parent ) {

			// echo title of parent and any breadcrumb signifier
			echo $ggp . $gp . $parent_echo . ' > ';

			// KLUDGE - create select dropdown
			echo '<select onchange="if (this.value) window.location.href=this.value">';

			// get the current title and output first
			$current = get_the_title($post->ID);
			echo '<option value="'.get_permalink($post->ID).'">'.$current.'</option>';

			// get the sibling pages as an array
			$pages = get_pages('child_of='.$parent.'');

			// run that foreach loop brah
			foreach ($pages as $page) {
				// save the title as $page_name
				$page_name = get_the_title($page->ID);

				// if the page name is not the same as the current page, proceed
				if($page_name != $current) {
					$item =  '<option value="'.get_permalink($page->ID).'">'.get_the_title($page->ID).'</option>';
					echo $item;
			// close it out
			echo '</select>';
		} else {
			// output the title if there is no parent

Get your parents, and your parents parents, and your parents parents parents. Such ancestry. Much age.


Notify of
Inline Feedbacks
View all comments

John Hartley is a Director of Product Engineering at Beam Dental in Columbus, OH. With 5+ years of leadership experience he has worked in startups, agencies, and began his career as a freelance Front End Developer. Always looking to iterate, this blog is a place for him to share his knowledge as well as hone his craft, challenge assumptions, and build a strong base of leadership and management knowledge. Connect with him on LinkedIn

Would love your thoughts, please comment.x