I’m completely new to custom post types and custom taxonomies, so bear with me. I’m trying to create a breadcrumb navigation for the custom taxonomy project-category
within the custom post type projects
. Essentially, I need it to work like this:
Custom Post Type > Custom Taxonomy Level 1 > Custom Taxonomy Level 2 > Custom Taxonomy Level 3 > etc.
Each heading needs to be linked to the corresponding page. I have this kind of working, but it’s not very dynamic, and it only links three levels of navigation. Here’s my code for that:
<ul>
<li><a href="https://wordpress.stackexchange.com/projects">Projects</a></li>
<?
$term = get_term_by("slug", get_query_var("term"), get_query_var("taxonomy") );
$parent = get_term($term->parent, get_query_var("taxonomy"));
if ($parent->name != "") {
echo "<li><a href=\"/project-category/" . $parent->slug . "\">" . $parent->name . "</a></li>";
}
if (is_tax("project-category")) {
$term = get_term_by("slug", get_query_var("term"), get_query_var("taxonomy"));
$tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
echo "<li><a href=\"/project-category/" . $term->slug . "\">" . $term->name . "</a></li>";
}
?>
</ul>
Up to the second level, this works currently, as below:
Custom Post Type > Taxonomy Level 1 > Taxonomy Level 2
But if I go to a third level, it ends up being:
Custom Post Type > Taxonomy Level 2 > Taxonomy Level 3
The deeper I go, the deeper the levels get off. I know the issue is that Im just checking for one parent, but I can’t figure out how to check for all possible parents.
UPDATE: I managed to get it working a bit better, but it’s listing things backwards. Here’s my updated code:
<ul>
<li><a href="https://wordpress.stackexchange.com/questions/149424/<? echo home_url(); ?>/projects">Projects</a></li>
<?
$term = get_term_by("slug", get_query_var("term"), get_query_var("taxonomy") );
$parent = get_term($term->parent, get_query_var("taxonomy"));
while ($parent->term_id) {
echo "<li><a href=\"" . home_url() . "/project-category/" . $parent->slug . "\">" . $parent->name . "</a></li>";
$parent = get_term($parent->parent, get_query_var("taxonomy"));
}
echo "<li><a href=\"" . home_url() . "/project-category/" . $term->slug . "\">" . $term->name . "</a></li>";
?>
</ul>
Instead of listing Custom Post Type > Taxonomy Level 1 > Taxonomy Level 2 > Taxonomy Level 3, it’s listing Custom Post Type > Taxonomy Level 2 > Taxonomy Level 1 > Taxonomy Level 3. It’s something to do with the while loop, but I can’t seem to reverse it.
Thanks.