Skip to content

Instantly share code, notes, and snippets.

@bobbydank
Created July 27, 2022 02:33
Show Gist options
  • Save bobbydank/c05e121585aeac22325f533145b46b5f to your computer and use it in GitHub Desktop.
Save bobbydank/c05e121585aeac22325f533145b46b5f to your computer and use it in GitHub Desktop.
Wordpress shortcode function for creating a breadcrumb
<?php
/**
* Get breadcrumb from current page
*
* usage : [my_breadcrumb]
* returns : HTML String
*/
function my_breadcrumb_func() {
global $post;
$string = '';
$ancestors = array_merge(
array(get_option('page_on_front')),
array_reverse($post->ancestors),
array($post->ID)
);
foreach ($ancestors as $key => $ancestor) {
$string .= ($key > 0) ? '<span class="marker">&raquo;</span>' : '';
$string .= '<a href="'.get_permalink($ancestor).'">'.get_the_title($ancestor).'</a>';
}
return '<p class="breadcrumb">'.$string.'</p>';
}
add_shortcode('my_breadcrumb', 'my_breadcrumb_func');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment