Skip to content

Instantly share code, notes, and snippets.

@rowatt
Created November 1, 2016 19:02
Show Gist options
  • Save rowatt/60e9eb3bdca7658f99f6286c88918576 to your computer and use it in GitHub Desktop.
Save rowatt/60e9eb3bdca7658f99f6286c88918576 to your computer and use it in GitHub Desktop.
Extend Genesis_Breadcrumb class to support hierarchical custom post types
<?php
/**
* Class to control breadcrumbs display. Adds hierachical display of custom post type breadcrumbs
* to the Genesis default.
*
* Note - to avoid conflicts this file must be loaded after the main Genesis_Breadcrumbs class has loaded,
* so the loading is triggered in class Site_Hooks.
*/
class Site_Breadcrumb extends Genesis_Breadcrumb {
public function __construct() {
parent::__construct();
}
/**
* Get breadcrumb for single custom post type entry, including any parent (CPT name) crumbs.
*
* @since 2.0.0
*
* @return string HTML markup for a single custom post type entry breadcrumb, including
* any parent (CPT name) breadcrumbs.
*/
protected function get_cpt_crumb() {
$post_type = get_query_var( 'post_type' );
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object->hierarchical ) {
return $this->get_hierarchical_cpt_crumb();
} else {
if ( $cpt_archive_link = get_post_type_archive_link( $post_type ) ) {
$crumb = $this->get_breadcrumb_link(
$cpt_archive_link,
'',
$post_type_object->labels->name
);
} else {
$crumb = $post_type_object->labels->name;
}
$crumb .= $this->args[ 'sep' ] . single_post_title( '', FALSE );
}
return $crumb;
}
/**
* Get breadcrumb for single hierarchical custom post type entry.
*
* @return string HTML markup for a single custom post type entry breadcrumb
*/
public function get_hierarchical_cpt_crumb() {
global $wp_query;
if ( $this->page_shown_on_front() && is_front_page() ) {
// Don't do anything - we're on the front page and we've already dealt with that elsewhere.
$crumb = $this->get_home_crumb();
} else {
$post = $wp_query->get_queried_object();
$post_type = get_query_var( 'post_type' );
$post_type_object = get_post_type_object( $post_type );
$crumbs = array();
if ( $cpt_archive_link = get_post_type_archive_link( $post_type ) ) {
$crumbs[] = $this->get_breadcrumb_link( $cpt_archive_link, '', $post_type_object->labels->name );
} else {
$crumbs[] = $post_type_object->labels->name;
}
// If this is a top level Page, it's simple to output the breadcrumb.
if ( ! $post->post_parent ) {
$crumbs[] = get_the_title();
} else {
if ( isset( $post->ancestors ) ) {
if ( is_array( $post->ancestors ) ) {
$ancestors = array_values( $post->ancestors );
} else {
$ancestors = array( $post->ancestors );
}
} else {
$ancestors = array( $post->post_parent );
}
foreach ( $ancestors as $ancestor ) {
$crumbs[] = $this->get_breadcrumb_link( get_permalink( $ancestor ), '', get_the_title( $ancestor ) );
}
// Add the current page title.
$crumbs[] = get_the_title( $post->ID );
}
$crumb = join( $this->args[ 'sep' ], $crumbs );
}
return $crumb;
}
/**
* Replace the standard genesis_do_breadcrumbs action with our own,
* but only if the standard hook is set.
*
* Note - if a theme inserts breadcrumbs elsewhere, we will need to
* manually change the hook to use our own breadcrumbs
*/
public static function init() {
if( has_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ) ) {
remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' );
add_action( 'genesis_before_loop', __CLASS__ . '::do_breadcrumbs', 9 );
}
}
/**
* Display Breadcrumbs above the Loop.
*/
public static function do_breadcrumbs( $args = array() ) {
if (
( ( 'posts' === get_option( 'show_on_front' ) && is_home() ) && ! genesis_get_option( 'breadcrumb_home' ) ) ||
( ( 'page' === get_option( 'show_on_front' ) && is_front_page() ) && ! genesis_get_option( 'breadcrumb_front_page' ) ) ||
( ( 'page' === get_option( 'show_on_front' ) && is_home() ) && ! genesis_get_option( 'breadcrumb_posts_page' ) ) ||
( is_single() && ! genesis_get_option( 'breadcrumb_single' ) ) ||
( is_page() && ! genesis_get_option( 'breadcrumb_page' ) ) ||
( ( is_archive() || is_search() ) && ! genesis_get_option( 'breadcrumb_archive' ) ) ||
( is_404() && ! genesis_get_option( 'breadcrumb_404' ) ) ||
( is_attachment() && ! genesis_get_option( 'breadcrumb_attachment' ) )
)
return;
global $_genesis_breadcrumb;
if ( ! $_genesis_breadcrumb )
$_genesis_breadcrumb = new Site_Breadcrumb;
$_genesis_breadcrumb->output( $args );
}
}
/* EOF */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment