Skip to content

Instantly share code, notes, and snippets.

@TCotton
Last active November 20, 2017 17:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TCotton/4723438 to your computer and use it in GitHub Desktop.
Save TCotton/4723438 to your computer and use it in GitHub Desktop.
Code for creating Wordpress breadcrumbs in a theme including custom taxonomy
<?php
// --> http://www.suburban-glory.com/blog?page=170
/*
* based on http://snipplr.com/view/57988/
*/
function get_term_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);
try {
if (is_wp_error($parent)) {
throw new Exception('is_wp_error($parent) has throw error ' . $parent->get_error_message());
}
}
catch (exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
// use something less drastic than die() in production code
//die();
}
if ($nicename) {
$name = $parent->slug;
} else {
$name = htmlspecialchars($parent->name, ENT_QUOTES, 'UTF-8');
}
if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
$visited[] = $parent->parent;
$chain .= get_term_parents($parent->parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
$chain .= '<a href="' . get_term_link($parent->slug, $taxonomy) . '">' . $name . '</a>' . $separator;
} else {
$chain .= $parent->name . $separator;
}
return $chain;
}
function get_tag_id($tag) {
global $wpdb;
$link_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $tag));
return $link_id;
}
function my_breadcrumb($id = null) {
echo '<a href="';
echo 'http://www.roedean.co.uk/';
echo '">';
echo 'Home';
echo "</a> &gt; ";
if (is_category() || is_single() || is_tax()) {
if (is_single()) {
$cat = get_the_category_list(' &gt; ', 'multiple');
// make sure uncategorised is not used
if (!stristr($cat, 'Uncategorized')) {
echo $cat;
echo ' &gt; ';
}
echo '<a href="' . get_permalink(get_the_ID()) . '">';
the_title();
echo '</a>';
}
if (is_category()) {
$cat = get_category_parents(get_query_var('cat'), true, ' &gt; ');
// remove last &gt;
echo preg_replace('/&gt;\s$|&gt;$/', '', $cat);
}
if (is_tax()) {
$tag = single_tag_title('', false);
$tag = get_tag_id($tag);
$term = get_term_parents($tag, get_query_var('taxonomy'), true, ' &gt; ');
// remove last &gt;
echo preg_replace('/&gt;\s$|&gt;$/', '', $term);
}
} elseif (is_page()) {
if ($id != null) {
$an = get_post_ancestors($id);
if(isset($an['0'])) {
$parent = '<a href="' . get_permalink($an['0']) . '">' . ucwords(get_the_title($an['0'])) . '</a>';
echo !is_null($parent) ? $parent . " &gt; " : null;
}
$parent = get_the_title($id);
$parent = '<a href="' . get_permalink($id) . '">' . ucwords($parent) . '</a>';
echo !is_null($parent) ? $parent . " &gt; " : null;
}
echo '<a href="' . get_permalink(get_the_ID()) . '">';
ucwords(the_title());
echo '</a>';
}
}
@omjatt
Copy link

omjatt commented Jun 7, 2017

so so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment