I breadcrumb tradizionali in WordPress vengono implementati per i post utilizzando i nomi e gli URL delle categorie. Per i custom post type il discorso รจ analogo, ma invece di utilizzare le categorie utilizzeremo la struttura delle tassonomie del custom post type.
Possiamo implementare la seguente funzione:
function my_display_cpt_breadcrumbs() {
global $post;
$id = $post->ID;
$term_list = wp_get_post_terms( $id, 'tassonomia', array( 'fields' => 'all' ) );
$html = '<ul class="cpt-breadcrumbs">';
$html .= '<li><a href="' . get_bloginfo( 'url' ) . '">Home</a></li>';
foreach( $term_list as $term ) {
$term_id = $term->term_id;
$link = get_term_link( $term_id, 'tassonomia' );
$name = $term->name;
$html .= '<li><a href="' . $link . '">' . $name . '</a></li>';
}
$html .= '<li><span>' . get_the_title( $id ) . '</span></li>';
$html .= '</ul>';
echo $html;
}
Si tratta solo di sostituire tassonomia
con l'abbreviazione (slug) della tassonomia di riferimento o di inserire tale tassonomia come parametro della funzione.