Like many others, I often use the Breadcrumb feature of the Yoast SEO plugin to output a breadcrumb on posts.
By default the label given to a post in the Breadcrumb is the post title. Sometimes I find myself wanting a different, usually shorter label in the breadcrumb.
For example, instead of “JobRelay Action Hooks” I just want the label in the breadcrumb to say “Actions”.
The code below, added to your themes functions.php
file or in a plugin will do just that.
<?php
/**
* Allows for a custom breadcrumb label.
*
* @param array $links The current breadcrumb links.
* @return array $links The modified breadcrumb links.
*/
function hd_edit_docs_breadcrumb( $links ) {
// if we have no links.
if ( empty( $links ) ) {
return $links;
}
// loop through each link.
foreach ( $links as $link_key => $link ) {
// if this link has an ID.
if ( ! empty( $link['id'] ) ) {
// get the custom link label from post meta.
$custom_label = get_post_meta( $link['id'], 'custom_breadcrumb_label', true );
// if we have a custom label.
if ( ! empty( $custom_label ) ) {
$links[ $link_key ]['text'] = $custom_label;
}
}
}
// return the breadcrumb.
return $links;
}
add_filter( 'wpseo_breadcrumb_links', 'hd_edit_docs_breadcrumb' );
To ensure a post has a custom breadcrumb label, add a custom field to the post with the key custom_breadcrumb_label
and the value is the label you want to show.