If on term-page -> get the current term?

I’m trying to write a kind of “breadcrumb” function in my “functions.php”

I’d like to query if I’m on a term-page and if so I want to print the current term I’m in.

By “term-page” I mean the following. I list all terms of a custom-taxonomy (associated with a custom-post-type) as kind of categories in my header. I do this with get_term_link($term->slug, 'my_taxonomy');

In my new function for the breadcrumbs I’d like to query if I’m on one of this pages and print this term.

First off, … I’m doing this …

if ( is_taxonomy_hierarchical('my_taxonomy) ) {
            echo "test";

But now my function is depending on this my_taxonomy string. I’d love to work this function for all upcoming taxonomies. How can I do this? There are conditional tags like is_category() that doesn’t need any params. Why do all term- or taxonomy-conditionals need this param?

And how can I print the current-term i’m in. Right now I’m just echoing “test” up there, however I’d like to print the current term.

Any ideas on that? Thank you in advance.

UPDATE:

function list_breadcrumbs() {

    $q_object = get_queried_object();
    $taxonomy = $q_object->taxonomy;

    $seperator = " <span class="separator">&rang;</span> ";

    if ( !is_home() ) {

            echo "<a href="" . get_bloginfo("url') . "' title="Home">Home</a>" . $seperator;

        if ( is_category() ) {
            $category = get_the_category(); 
            echo "<span class="breadcrumb">" . $category[0]->cat_name . "</span>";
        } else if ( is_tax( $taxonomy ) ) {
            echo "The Term Name";
        } else if ( is_single() ) {
            …
        } else if ( is_page() ) {
            …

1 Answer
1

You’ll want get_queried_object(). This is a very generic function – and simply returns the queried object- so a single post, this would be a post object.

For instance, the return object may be of the form:

Object (
    [term_id] => 299
    [name] => test
    [slug] => test
    [term_group] => 0
    [term_taxonomy_id] => 317
    [taxonomy] => event-category
    If on term-page -> get the current term? => 
    [parent] => 0
    [count] => 2
)

So for instance:

  $q_object = get_queried_object();
  if( isset($q_object->taxonomy) ){
     $taxonomy = $q_object->taxonomy;
  }else{
    //Not a taxonomy page
  }

To use this in a function:

function wpse51753_breadcrumbs(){
    if( !is_tax() && !is_tag() && !is_category() )
       return;

    //If we got this far we are on a taxonomy-term page
    // (or a tag-term or category-term page)
    $taxonomy = get_query_var( 'taxonomy' );
    $queried_object = get_queried_object();
    $term_id =  (int) $queried_object->term_id;

    //Echo breadcrumbs
}

Then just wpse51753_breadcrumbs(); in your template wherever you want to display the breadcrumbs.

Leave a Comment