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.
2 Answers
Using your method, I’ve edited it a bit to make it more viable. Here’s the code, below I’ll explain.
<ul>
<li><a href="https://wordpress.stackexchange.com/questions/149424/<?php echo home_url(); ?>/projects">Projects</a></li>
<?php
$term = get_term_by("slug", get_query_var("term"), get_query_var("taxonomy") );
$tmpTerm = $term;
$tmpCrumbs = array();
while ($tmpTerm->parent > 0){
$tmpTerm = get_term($tmpTerm->parent, get_query_var("taxonomy"));
$crumb = '<li><a href="' . get_term_link($tmpTerm, get_query_var('taxonomy')) . '">' . $tmpTerm->name . '</a></li>';
array_push($tmpCrumbs, $crumb);
}
echo implode('', array_reverse($tmpCrumbs));
echo '<li><a href="' . get_term_link($tmpTerm, get_query_var('taxonomy')) . '">' . $term->name . '</a></li>';
?>
</ul>
After you get the term I assign it to a temporary term viable so I can overwrite it. IF that term has a parent we go into the loop and get the original terms parent, but we assign it to our temporary term because we need to boil up to the highest term, so when the loop runs again it checks if term2 has a parent. We store the HTML in an array because it’s easier to reverse and display in PHP. If you have a question about the above let me know, hopefully it works for you!
Oh! Also you should let WordPress take care of getting the term links for you, it’s much more reliable if slugs change.