Skip to content

Commit

Permalink
Merge pull request #109 from globeandmail/fix/hierarchy-taxonomy-data
Browse files Browse the repository at this point in the history
Fix: only use public hierarchial taxonomy for breadcrumb
  • Loading branch information
jeffpaul authored Jul 23, 2021
2 parents 6fff3b6 + ac275fe commit 3d0d044
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions includes/functions/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,28 @@ function( $parent ) {
// If the current post isn't hierarchical, we use taxonomy.
$taxonomies = array_filter(
get_object_taxonomies( $post ),
'is_taxonomy_hierarchical'
function( $taxonomy ) {
$taxonomy_object = get_taxonomy( $taxonomy );
return $taxonomy_object->hierarchical && $taxonomy_object->public && $taxonomy_object->publicly_queryable;
}
);

if ( count( $taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $taxonomies[0] );
/**
* Filter the hierarchial taxonomy to use to create breadcrumb. Default to the first
* public and hierarchial taxonomy attached to the post.
*
* @since 1.0.4
*
* @hook sophi_hierarchial_taxonomy_for_breadcrumb
*
* @param {string} $taxonomy Taxonomy used for breadcrumb.
* @param {WP_Post} $post Post object.
*
* @return {string} Taxonomy used for breadcrumb..
*/
$taxonomy = apply_filters( 'sophi_hierarchial_taxonomy_for_breadcrumb', $taxonomies[0], $post );
$terms = get_the_terms( $post, $taxonomy );

if ( count( $terms ) > 0 ) {
return get_term_breadcrumb( $terms[0] );
Expand All @@ -74,11 +91,27 @@ function( $parent ) {
$non_hierarchial_taxonomies = array_filter(
get_object_taxonomies( $post ),
function( $taxonomy ) {
return ! is_taxonomy_hierarchical( $taxonomy );
$taxonomy_object = get_taxonomy( $taxonomy );
return ! $taxonomy_object->hierarchical && $taxonomy_object->public && $taxonomy_object->publicly_queryable;
}
);

if ( count( $non_hierarchial_taxonomies ) > 0 ) {
$terms = get_the_terms( $post, $non_hierarchial_taxonomies[0] );
/**
* Filter the non hierarchial taxonomy to use to create breadcrumb. Default to the first
* public and non hierarchial taxonomy attached to the post.
*
* @since 1.0.4
*
* @hook sophi_non_hierarchial_taxonomy_for_breadcrumb
*
* @param {string} $taxonomy Taxonomy used for breadcrumb.
* @param {WP_Post} $post Post object.
*
* @return {string} Taxonomy used for breadcrumb..
*/
$taxonomy = apply_filters( 'sophi_non_hierarchial_taxonomy_for_breadcrumb', $non_hierarchial_taxonomies[0], $post );
$terms = get_the_terms( $post, $taxonomy );

if ( count( $terms ) > 0 ) {
return $terms[0]->slug;
Expand Down

0 comments on commit 3d0d044

Please sign in to comment.