diff --git a/.gitignore b/.gitignore index 61ead86..2e07007 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor +.vscode diff --git a/src/Crumb.php b/src/Crumb.php index f1f8a8c..2a9c718 100644 --- a/src/Crumb.php +++ b/src/Crumb.php @@ -199,50 +199,66 @@ public function build() ); } - if (is_singular('post')) { - $categories = get_the_category(get_the_ID()); + if (is_singular()) { + $postType = get_post_type(); + $postTypeObject = get_post_type_object($postType); + + // Add post type archive link if it exists + if ($postTypeObject && !empty($postTypeObject->has_archive)) { + $this->add( + $postTypeObject->label, + get_post_type_archive_link($postType) + ); + } - if (! empty($categories)) { - foreach ($categories as $index => $category) { - $this->add( - $category->name, - get_category_link($category), - $category->term_id, - $index === 0 - ); + // Add custom taxonomy terms + $taxonomies = get_object_taxonomies($postType, 'objects'); + foreach ($taxonomies as $taxonomy) { + if ($taxonomy->public) { + $terms = get_the_terms(get_the_ID(), $taxonomy->name); + if (!empty($terms) && !is_wp_error($terms)) { + if ($taxonomy->hierarchical) { + // Handle hierarchical taxonomies + $term = array_shift($terms); // Get the first term + $ancestors = get_ancestors($term->term_id, $taxonomy->name); + + // Add ancestor terms + foreach (array_reverse($ancestors) as $ancestorId) { + $ancestor = get_term($ancestorId, $taxonomy->name); + $this->add( + $ancestor->name, + get_term_link($ancestor, $taxonomy->name), + $ancestor->term_id + ); + } + + // Add the current term + $this->add( + $term->name, + get_term_link($term, $taxonomy->name), + $term->term_id + ); + } else { + // Handle non-hierarchical taxonomies + foreach ($terms as $term) { + $this->add( + $term->name, + get_term_link($term, $taxonomy->name), + $term->term_id + ); + } + } + } } - - return $this->add( - get_the_title(), - null, - get_the_ID() - ); } + // Add the current post return $this->add( get_the_title(), null, - get_the_ID(), - true - ); - } - - $type = get_post_type_object( - get_post_type() - ); - - if (! empty($type)) { - $this->add( - $type->label, - get_post_type_archive_link($type->name), - get_queried_object_id() + get_the_ID() ); } - - return $this->add( - get_the_title(), - null, - get_the_ID() - ); + return $this->breadcrumb; } }