PHP code example of tyxla / carbon-breadcrumbs
1. Go to this page and download the library: Download tyxla/carbon-breadcrumbs library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tyxla / carbon-breadcrumbs example snippets Carbon_Breadcrumb_Trail::output();
Carbon_Breadcrumb_Trail::output(array(
'glue' => ' > ', // glue between breadcrumb items
'link_before' => '',
'link_after' => '',
'wrapper_before' => '',
'wrapper_after' => '',
'title_before' => '',
'title_after' => '',
'min_items' => 2,
'last_item_link' => true,
'display_home_item' => true,
'home_item_title' => __('Home', 'carbon_breadcrumbs'),
'renderer' => 'Carbon_Breadcrumb_Trail_Renderer',
));
$trail = new Carbon_Breadcrumb_Trail(array(
'glue' => ' > ',
'link_before' => '',
'link_after' => '',
'wrapper_before' => '',
'wrapper_after' => '',
'title_before' => '',
'title_after' => '',
'min_items' => 2,
'last_item_link' => true,
'display_home_item' => true,
'home_item_title' => __('Home', 'carbon_breadcrumbs'),
'renderer' => 'Carbon_Breadcrumb_Trail_Renderer',
));
$trail->setup(); // setup the trail by generating necessary breadcrumb items
$trail->render(); // display the breadcrumb trail
# A custom breadcrumb trail renderer class
class Crb_Carbon_Breadcrumb_Trail_Renderer extends Carbon_Breadcrumb_Trail_Renderer {
// your custom renderer methods & overrides here
}
# Modify the default breadcrumb trail renderer class
function crb_carbon_breadcrumbs_renderer_class($renderer_class) {
return 'Crb_Carbon_Breadcrumb_Trail_Renderer';
}
# Prepend "Blog Post: " to the title of the post items
add_filter('carbon_breadcrumbs_item_title', 'crb_carbon_breadcrumbs_item_title_post', 10, 2);
function crb_carbon_breadcrumbs_item_title_post($item_title, $item) {
if ( is_a($item, 'Carbon_Breadcrumb_Item_Post') && $item->get_subtype() == 'post' ) {
$item_title = 'Blog Post: ' . $item_title;
}
return $item_title;
}
# Append "?test=1" to the link of the post items
add_filter('carbon_breadcrumbs_item_link', 'crb_carbon_breadcrumbs_item_link_post', 10, 2);
function crb_carbon_breadcrumbs_item_link_post($item_link, $item) {
if ( is_a($item, 'Carbon_Breadcrumb_Item_Post') && $item->get_subtype() == 'post' ) {
$item_link = add_query_arg('test', 1, $item_link);
}
return $item_link;
}
# Add a new custom item by specifying title, link and priority
add_action('carbon_breadcrumbs_after_setup_trail', 'crb_add_custom_item');
function crb_add_custom_item($trail) {
$trail->add_custom_item('Link Title', 'http://example.com/custom/link/', 500);
}
# Add a new page item and its ancestry
add_action('carbon_breadcrumbs_after_setup_trail', 'crb_add_test_page_item');
function crb_add_test_page_item($trail) {
$locator = Carbon_Breadcrumb_Locator::factory('post', 'page');
$priority = 500;
$page_id = 123;
$items = $locator->get_items($priority, $page_id);
if ($items) {
$trail->add_item($items);
}
}
# Remove the "Electronics" category item
add_action('carbon_breadcrumbs_after_setup_trail', 'crb_remove_electronics_category_item', 500);
function crb_remove_electronics_category_item($trail) {
$trail->remove_item('Electronics', 'http://example.com/category/electronics/');
}
# Remove all items with priority 999
add_action('carbon_breadcrumbs_after_setup_trail', 'crb_remove_items_priority_999', 500);
function crb_remove_items_priority_999($trail) {
$trail->remove_item_by_priority(999);
}
# Initialize the trail as bulleted list
$trail = new Carbon_Breadcrumb_Trail(array(
'glue' => '',
'link_before' => '<li>',
'link_after' => '</li>',
'wrapper_before' => '<ul>',
'wrapper_after' => '</ul>',
'title_before' => '',
'title_after' => '',
'min_items' => 1,
'last_item_link' => true,
'display_home_item' => true,
'home_item_title' => __('Home', 'carbon_breadcrumbs'),
'renderer' => 'Carbon_Breadcrumb_Trail_Renderer',
));
# Setup the trail by generating necessary breadcrumb items
$trail->setup();
# Display the breadcrumb trail
$trail->render();