Skip to content

Instantly share code, notes, and snippets.

@jmcclellan
Last active December 18, 2015 00:59
Show Gist options
  • Save jmcclellan/5700490 to your computer and use it in GitHub Desktop.
Save jmcclellan/5700490 to your computer and use it in GitHub Desktop.
Outputs a category breadcrumb with a parent/child relationship. This code will only work flawlessly if there is only one category selected on the post.
<?php
function somesite_cat_breadcrumb() {
// variables
$category = get_the_category();
$catParID = $category[0]->category_parent;
$catParent = get_cat_name($catParID);
$catName = $category[0]->cat_name;
// filter breadcrumb for posts with no parent categories versus those with 1 or more parent categories and
// then display the immediate parent category (if applicable) followed by the current (or first) post category
if( $catParID == 0 ) {
echo '<span>'.$catName.'</span>';
} else {
// category breadcrumb output
echo '<a href="'.get_category_link($catParID).'">'.$catParent.'</a>';
echo ' / ';
echo '<span>'.$catName.'</span>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment