Skip to main content

Custom breadcrumb list in wordpress

While developing a custom plugin for a website I needed to post a custom breadcrumb trail.

Articles on this website are based on a custom post type and some of them were very deep in hierarchical order.  (Example : Knowledgebase > Indian Mining Regulations > DGMS > Mining Legislation > Safety Conferences on Safety in Mines > X - Safety Conference)

I wanted to put a series of links for visitor to follow the progression of contents. So, I thought about giving them breadcrumb trails to follow it back to wherever they want to go in the series. After some google search and some modifications: here is what I created. Hope, it may help you. This is for a custom post type of hierarchical in nature, but it can also be used for pages or other post types having hierarchies.


function fn_nova_breadcrumb_trail($post)
{
$separator = '  > ';
$trail_posts = array($post);
$parent = $post;
$current_post_id=$post->ID;
while($parent->post_parent)
{
  $parent = get_post( $parent->post_parent );
  array_unshift( $trail_posts, $parent );
}
$str_return='';
foreach($trail_posts as $trail_post)
{
  $trail_post_id=$trail_post->ID;
 
if($current_post_id==$trail_post_id)
{
$str_return.= $separator.$trail_post->post_title;
}
else
{
$str_return.= $separator.'<a href="'.get_permalink($trail_post_id).'">'.$trail_post->post_title.'</a>';
}
}
return $str_return;
}

If need be, you can easily convert this into a nice shortcode. You can also support custom taxonomies.

Ref. WordPress Breadcrumb Navigation

Comments