WordPress.org

Ready to get started?Download WordPress

Forums

Automatic 'parent category' -> 'custom taxonomy term' breadcrumb code (6 posts)

  1. design_dolphin
    Member
    Posted 4 years ago #

    The code below automatically selects the breadcrumb trail for the category, and custom taxonomy.

    The most important bit for me was how to figure out, how to choose a custom taxonomy term automatically, based on the taxonomy term url. As well as how to get the url of just a parent category.

    The breadcrumb trail has the custom taxonomy term after a parent category:

    Home -> Parent category -> Custom taxonomy term

    You might also be able to do it as:

    Home -> Custom taxonomy -> Taxonomy term

    However when I tried to do it this way and linked to blog.com/custom_taxonomy, the link gave a 404 error. As I am using a different set-up (category -> taxonomy term) in the first place, and doing url rewriting in both the theme and the Apache paths, it is not something I need at the the moment so I did not spent a lot of time trying to figure out. But feel free to do so.

    It took me awhile to figure this out, but I hope it helps others on their way.

    Please do feel free to improve and customize. This is a beta code snippet. Use at your own risk. I would not use this code snippet on a live site yet. The code is more meant for people trying to figure their way out in coding with automatic category/ custom taxonomies selection and breadcrumbs, and to build from there if and when needed.

    Code:

    <?php 
    
     $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
     $tax_term_breadcrumb_term_slug =  $term->slug;
     $tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
    
         foreach (get_the_category() as $cat) {
            $parent = get_category($cat->category_parent);
            $parent_name = $parent->cat_name;
            $parent_slug = $parent_name;
            $parent_slug = strtolower(str_replace("(","",$parent_slug));
            $parent_slug = str_replace(')',' ', $parent_slug);
            $parent_slug = str_replace(' ','-', $parent_slug);
                                               }
         if ( is_category($parent_name)  ) { ?>
    
            <a href="<?php echo get_option('home'); ?>/" title="Home">Home</a> » <? echo $parent_name; ?>
    
    <?php } elseif (is_tax($tax_term_breadcrumb_term_slug)) { ?>
    
    <a href="<?php echo get_option('home'); ?>/" title="Home">Home</a> » <a href="<? echo get_option('home')?>/<? echo $parent_slug ?>" title="<? echo $parent_name;" ?>"><? echo $parent_name; ?></a> » <?php echo $term->name; ?>
    
    <?php } elseif ( is_front_page() ){ ?>
    Home
    <? } ?>

    Hope it helps. :-) Suggestions and feedback are welcome.

    Edit: The forum automatically converted the » in the code above.
    The original code was &.#.1.8.7.;. (without the dots) instead of the »

  2. apatheticresistance
    Member
    Posted 4 years ago #

    Thank you so much for this!

    I used it to figure out how to make a is_category() type archive page. I mean, wordpress already builds a permalinked archive for custom taxonomy terms, but I couldn't find a way to put a page title on the archive. So, unless you look in the URL, it just looks like a random assortment of posts.

    But I used your code to output the custom taxonomy term within an H1, and also within the TITLE tag.

    For anyone who is interested, here's the code for the H1, which I basically just rearranged from your code up top:

    if (is_tax('place')) {
    	$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    	$tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
    	echo '<h1>' . $term->name . '</h1>';
    }
  3. Taylor Baybutt
    Member
    Posted 4 years ago #

    wow, finally finding this was so friggin hard. custom taxonomies definitely needs to get a single_term_title function for 3.0. Thanks so much for this thread. I want to list a few things here so that it is more easily searched for.

    single_tag_title single_term_title single_tax_title single_taxonomy_title single_cat_title "custom taxonomy" "title" template tag taxonomy.php

  4. leaf.
    Member
    Posted 3 years ago #

    What about meta title and description tags on custom taxonomy posts. Anybody know how to do this?

    There are a couple of plugin which enable basic and generic titles, but I really need to specify to the child level.

    So where my brands taxonomy includes Nike, Puma, Reebok, can I have unique titles/desc for the individual brands / children of ?

  5. codestripper
    Member
    Posted 3 years ago #

    I can't thank you and apatheticresistance enough for this code. I've been struggling with what seems like a simple query or writing a custom function that gets and outputs the 'nicename' for a taxonomy slug on category or archive templates. A lot of others have had problems with this too so I added more tags to this thread.

  6. moffepoffe
    Member
    Posted 3 years ago #

    Hey,

    Im trying to use this code but it isnt working, im running WP 3.0.1.

    It doesnt give me any output at all.

    Thanks in advance,
    marten

Topic Closed

This topic has been closed to new replies.

About this Topic