Skip to content

Instantly share code, notes, and snippets.

@TCotton
Last active December 12, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TCotton/4714817 to your computer and use it in GitHub Desktop.
Save TCotton/4714817 to your computer and use it in GitHub Desktop.
Code for creating Wordpress breadcrumbs in a theme
<?php
/* explanation of code here: http://www.suburban-glory.com/blog?page=170 */
function my_breadcrumb($id = null) {
echo '<a href="';
echo get_option('home');
echo '">';
echo 'Home';
echo "</a> &gt; ";
if (is_category() || is_single()) {
if (is_single()) {
the_title();
}
if(is_category()) {
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
$cat = get_category_parents($cat_id, TRUE, ' &gt; ');
// remove last &gt;
echo preg_replace('/&gt;\s$/', '', $cat);
}
} elseif (is_page()) {
if ($id != null) {
$parent = get_the_title($id);
$parent = '<a href="' . get_permalink($id) . '">' . $parent . '</a>';
echo !is_null($parent) ? $parent . "&gt; " : null;
}
echo the_title();
}
}
?>
<div id="breadcrumb">
<?php if (!is_search() || !is_404()) {
global $post;
if ($post != null) {
my_breadcrumb($post->post_parent);
} else {
my_breadcrumb();
}
} else {
print '&nbsp;';
} ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment