WordPress: Adding custom breadcrumbs

This may not be the most elegant solution, but has nicely fitted the WordPress website I am currently developing. The brief required that we have a general “Tours” landing page that links to two subcategories (custom post types) – “Mike’s Tours” and “Sally’s Tours”. Using custom post types has allowed us to customise each of Mike’s and Sally’s pages heavily without extensive hacking of the wordpress core functionality.

What we required on each tour detail page was a set of breadcrumbs that linked back to the relevant tour type. There are plenty of breadcrumb plugins available, however none that suited our requirements or that made for easy customisation. The following is what we went for on our custom page template…

Smug Piggy for the Latest Men's & Women's T-shirts
$breadcrumbs = array();

$breadcrumbs[] = array(
'text' => 'Home',
'url' => home_url()
);

$breadcrumbs[] = array(
'text' => 'All Tours',
'url' => home_url() . '/tours'
);

// get custom post breadcrumb
$referer = wp_get_referer();
$pattern1 = "/(mikes-tours)\/$/i";
$pattern2 = "/(sallys-tours)\/$/i";

if (preg_match($pattern1, $referer)) {

$breadcrumbs[] = array(
'text' => 'Mike\'s Tours',
'url' => home_url() . '/mikes-tours'
);

} else if (preg_match($pattern2, $referer)) {

$breadcrumbs[] = array(
'text' => 'Sally\'s Tours',
'url' => home_url() . '/sallys-tours'
);
}

// get current page breadcrumb using core WP functions
global $wp;
$breadcrumbs[] = array(
'text' => get_the_title(),
'url' => home_url( add_query_arg(array(), $wp->request) )
);

get_header();
?>

<!-- Breadcrumbs -->
<div class="container">
<ul class="breadcrumbs">
<?php foreach($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['url']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *