This is a WordPress plugin. Official download available on wordpress.org.
Generate a breadcrumb trail from a WordPress Menu
Breadcrumbs are often generated from Page structure, but in a world of Custom Post Types that doesn't always work. Menu Breadcrumb uses your WordPress Menu to generate a breadcrumb trail based on the current page.
- Download the plugin and extract the files
- Upload
menu-breadcrumb
to your~/wp-content/plugins/
directory - Activate the plugin through the 'Plugins' menu in WordPress
After activating, add breadcrumbs by inserting the following in you template file:
<?php if ( function_exists( 'menu_breadcrumb') ) { menu_breadcrumb( 'main' ); } ?>
where 'main'
is the Location of the Menu from which you want to generate the breadcrumb trail.
Full usage (all arguments)
<?php
if ( function_exists( 'menu_breadcrumb') ) {
menu_breadcrumb(
'main', // Menu Location to use for breadcrumb
' » ', // separator between each breadcrumb
'<p class="menu-breadcrumb">', // output before the breadcrumb
'</p>' // output after the breadcrumb
);
}
?>
If you would like to utilize Menu Breadcrumb more granularly you can use the class directly.
<?php
$menu_breadcrumb = new Menu_Breadcrumb( 'main' ); // 'main' is the Menu Location
$menu_breadcrumb->render( ' » ', '<p class="menu-breadcrumb">', '</p>' );
<?php
$menu_breadcrumb = new Menu_Breadcrumb( 'main' ); // 'main' is the Menu Location
$breadcrumb_array = $menu_breadcrumb->generate_trail();
print_r( $breadcrumb_array ); // array of WP_Post objects in order of breadcrumb
<?php
$menu_breadcrumb = new Menu_Breadcrumb( 'main' ); // 'main' is the Menu Location
$breadcrumb_array = $menu_breadcrumb->generate_trail();
$breadcrumb_markup = $menu_breadcrumb->generate_markup( $breadcrumb_array, ' » ' );
echo '<p class="menu-breadcrumb">' . $breadcrumb_markup . '</p>';
<?php
$menu_breadcrumb = new Menu_Breadcrumb( 'main' ); // 'main' is the Menu Location
$current_menu_item_object = $menu_breadcrumb->get_current_menu_item_object();
Filter each breadcrumb item. By default Menu Breadcrumb will wrap each item as follows:
<span typeof="v:Breadcrumb"><a href="{Menu Item URL}">{Menu Item Title}</a></span>
If you would like to disable that, you can remove all filters before adding your own:
<?php
// remove core Menu Breadcrumb item markup filter
remove_all_filters( 'menu_breadcrumb_item_markup' );
// add my own Menu Breadcrumb item filter
function my_menu_breadcrumb_item_markup( $markup, $breadcrumb ) {
// $markup is in the format of <a href="{Menu Item URL}">{Menu Item Title}</a>
// $breadcrumb is the Menu item object itself
return '<span class="breadcrumb-item">' . $markup . '</span>';
}
add_filter( 'menu_breadcrumb_item_markup', 'my_menu_breadcrumb_item_markup', 10, 2 );
Filter the entire breadcrumb markup before it is returned. By default it is wrapped as follows:
<span prefix="v: http://rdf.data-vocabulary.org/#">{Markup for all breadcrumbs after they have been filtered}</span>
If you would like to disable that, you can remove all filters before adding your own:
<?php
// remove core Menu Breadcrumb markup filter
remove_all_filters( 'menu_breadcrumb_markup' );
// add my own Menu Breadcrumb markup filter
function my_menu_breadcrumb_markup( $markup ) {
return '<p class="my-breadcrumbs">' . $markup . '</p>';
}
add_filter( 'menu_breadcrumb_markup', 'my_menu_breadcrumb_markup' );
<dt>1.0.2</dt>
<dd>Added a <code>menu_breadcrumb_level</code> property to each breadcrumb object</dd>
<dt>1.0.1</dt>
<dd>Fixed an issue where the Menu wasn't properly retrieved from the location</dd>
<dt>1.0.0</dt>
<dd>Initial release</dd>