1

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="/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="<? 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.

  • 1
    You're going to need a loop of some kind which will keep looping until it hits a stopping piont of $term->parent == 0 <--- that's your stopping point, where there's no more parents to get. – Howdy_McGee Jun 12 '14 at 16:40
  • Seriously I hate throwing plugins for answer, but i'm using Hansel & Gretel: Fine-Tuned Breadcrumb Generator and I really like it, since i'm using it, no more headache for breadcrumbs ;) – Jonathan Lafleur Jun 12 '14 at 17:16
  • @Howdy_McGee please see my updated question. I've got it working, but backwards. – JacobTheDev Jun 12 '14 at 18:19
5

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="<?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.

1

Figured it out about 10 minutes before @Howdy_McGee posted, but I was able to use some of their code to improve mine. Here's what I ended up using;

$term = get_term_by("slug", get_query_var("term"), get_query_var("taxonomy") );
$parent = get_term($term->parent, get_query_var("taxonomy"));
$parentItems = array();
while ($parent->term_id) {
    array_push($parentItems,"<li><a href=\"" . home_url() . "/project-category/" . $parent->slug . "\">" . $parent->name . "</a></li>");
    $parent = get_term($parent->parent, get_query_var("taxonomy"));
}
echo implode (array_reverse($parentItems));
echo "<li><a href=\"" . home_url() . "/project-category/" . $term->slug . "\">" . $term->name . "</a></li>";
  • Your answer should be a edition of @Howdy_McGee answer. Also, Howdy_McGee's answer is still better; for example, you are not using the correct functions to get the term link. – cybmeta Jun 12 '14 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.