Created
January 20, 2018 08:23
-
-
Save natenault/6ffeeec36252aee62e7b73efeaedc79e to your computer and use it in GitHub Desktop.
Filter the Genesis CPT breadcrumb for hierarchical CPT to include ancestor pages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Filter the Genesis CPT breadcrumb. | |
* | |
* @since 2.5.0 | |
* | |
* @param string $crumb HTML markup for the CPT breadcrumb. | |
* @param array $args Arguments used to generate the breadcrumbs. Documented in Genesis_Breadcrumbs::get_output(). | |
*/ | |
function nn_cpt_breadcrumb( $crumb, $args ) { | |
global $wp_query; | |
if ( !is_singular( 'CPT_SLUG' ) ) { | |
return $crumb; | |
} | |
$post = $wp_query->get_queried_object(); | |
// If this is a top level Page, it's simple to output the breadcrumb. | |
if ( ! $post->post_parent ) { | |
$crumb = get_the_title(); | |
} else { | |
// Get the IDs of all parents and ancestors in an array. | |
$ancestors = get_post_ancestors( $post->ID ); | |
// Add ancestor breacrumb links to $crumbs array | |
// get_breadcrumb_link() https://gist.github.com/natenault/af861546ab8a3468d12a09e89437ed54 | |
$crumbs = array(); | |
foreach ( $ancestors as $ancestor ) { | |
array_unshift( | |
$crumbs, | |
nn_get_breadcrumb_link( get_permalink( $ancestor ), '', get_the_title( $ancestor ) ) | |
); | |
} | |
// Add the current page title. | |
$crumbs[] = get_the_title( $post->ID ); | |
$crumb = implode( $args['sep'], $crumbs ); | |
} | |
return $crumb; | |
} | |
add_filter( 'genesis_cpt_crumb', 'nn_cpt_breadcrumb', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment