Skip to content

Instantly share code, notes, and snippets.

@bosconian-dynamics
Created April 10, 2014 23:09
Show Gist options
  • Save bosconian-dynamics/10430974 to your computer and use it in GitHub Desktop.
Save bosconian-dynamics/10430974 to your computer and use it in GitHub Desktop.
WordPress breadcrumb template
<?php
/**
* Breadcrumb PluginComponent template.
*
* @TODO: this is an incredibly hackish template and should likely be killed with fire, rebuilt.
* @TODO: all breadcrumbs involving date and time functions need to have a configurable format
* @TODO: support for internationalization
*
* @var string $containerElement
* @var string $containerClass
* @var string $containerId
* @var string $homeUri
* @var string $homeText
* @var string $beforeHome
* @var string $afterHome
* @var string $delimiter
* @var string $crumbsWrapperElement
* @var string $crumbsWrapperClass
* @var string $crumbsWrapperId
* @var string $crumbBefore
* @var string $crumbAfter
* @var string $crumbElement //@TODO: implement $crumbElement so list markup can be constructed
* @var bool $showPageNumber
* @var array[ string ] $text
* ['searchResults']
* ['authorArchive']
* ['tagArchive']
* ['categoryArchive']
* ['pagination']
* ['404']
*/
echo( '<' . $containerElement );
if( isset( $containerClass ) )
echo ( ' class="' . $containerClass . '"');
echo('>');
/** @var WP_POST $post */
global $post;
$currentAnchor = isset( $currentClass ) && ! empty( $currentClass ) ? '<a href="#" class="' . $currentClass . '">' : '';
echo( '<a href="' . $homeUri . '">' . $beforeHome . $homeText . $afterHome . '</a>' );
if( ! is_home() && ! is_front_page() ) {
echo( $delimiter );
if (is_category()) {
/** @var WP_Query $wp_query */
global $wp_query;
$category = $wp_query->get_queried_object();
$thisCat = get_category( $category->term_id );
$parentCat = get_category( $thisCat->parent );
$ancestorCatString = $thisCat->parent !== 0 ? $thisCat->get_category_parents( $parentCat, TRUE, $delimiter ) : ''; //@TODO: error handling - get_cat_parents can return WP_Error
echo($ancestorCatString . $currentAnchor . str_replace( '{categoryTitle}', single_cat_title(), $text['categoryArchive']) . '</a>' );
} elseif (is_day()) {
$year = get_the_time('Y');
echo('<a href="' . get_year_link($year) . '">' . $year . '</a>' . $delimiter );
echo('<a href="' . get_month_link($year, get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $delimiter );
echo($currentAnchor . get_the_time('d') . '</a>');
} elseif (is_month()) {
$year = get_the_time('Y');
echo('<a href="' . get_year_link($year) . '">' . $year . '</a>' . $delimiter );
echo($currentAnchor . get_the_time('F') . '</a>');
} elseif (is_year()) {
echo($currentAnchor . get_the_time('Y') . '</a>');
} elseif (is_single()) {
$cat = get_the_category()[0];
$ancestorCatString = get_category_parents( $cat, TRUE, $delimiter );
if (!is_string($ancestorCatString))
$ancestorCatString = ''; //@TODO: Ignorance is bliss, but error handling is better...
echo( $ancestorCatString . $currentAnchor . the_title() . '</a>' );
} elseif (is_page()) {
if ($post->post_parent) {
$breadcrumbs = array();
$parent_id = $post->post_parent;
while ($parent_id) {
$page = get_post($parent_id);
$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb)
echo $crumb . $delimiter;
}
echo($currentAnchor . the_title() . '</a>');
} elseif (is_search()) {
echo($currentAnchor . str_replace('{searchQuery}', get_search_query(), $text['searchResults']) . '</a>');
} elseif (is_tag()) {
echo($currentAnchor . str_replace('{tagTitle}', single_tag_title(), $text['tagArchive']) . '</a>');
} elseif (is_author()) {
global $author;
//@TODO: abstract templatable string handling into component... Potentially PluginComponent class itelf
/** @var WP_User $userdata */
$userdata = get_userdata($author);
$textTags = array(
'{displayName}' => $userdata->display_name,
'{firstName}' => $userdata->user_firstname,
'{lastName}' => $userdata->user_lastname,
'{name}' => $userdata->user_firstname . ' ' . $userdata->user_lastname,
'{url}' => $userdata->user_url,
'{email}' => $userdata->user_email,
'{nickname}' => $userdata->nickname,
'{description}' => $userdata->description
);
echo($currentAnchor . str_replace(array_keys($textTags), $text['authorArchive'], array_values($textTags)) . '</a>');
} elseif (is_404()) {
echo($currentAnchor . $text['404'] . '</a>');
}
if ($showPageNumber && get_query_var('paged'))
echo(str_replace('{pageNumber}', get_query_var('paged'), $text['paged']));
echo '</' . $containerElement . '>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment