WordPress Breadcrumbs – Jankified

Posted on: April 4th, 2014

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.

	<?php 
		
		// 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
			the_title();
		}
	?>

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

breadcrumbs

previous post: Sticky Nav After Scroll – Olly McTwist 580 next post: Desktop Background #3