2

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

8

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
    [description] => 
    [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.

5
  • Thank you! I updated my question above in order for you to see my current code. The problem with this is that if I'm not on a tax/term page I get this error Notice: Undefined property: stdClass::$taxonomy in … functions.php - So as you see in my sample above, I simply want to have a conditional for my term-page. "If on termpage -> print the term" Any ideas? May 11, 2012 at 5:55
  • That's because the taxonomy property won't be set if the queried object isn't a term. See updated answer. May 11, 2012 at 12:32
  • Thank you, but in this case do I have to wrap my entire breadcrumbs function inside of this isset-condition? I don't get how I could most efficiently apply those lines to my code above. Would you mind showing me this depending on my sample code above. Thank you in advance for your awesome help. May 11, 2012 at 13:59
  • awesome help added ::). I've used some functions rather than interacting with the queried object directly. May 11, 2012 at 14:08
  • Any tips for when querying multple categories at a time, like at this URL: example.com/category/cars+bikes/?
    – Drew Baker
    Sep 13, 2015 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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