Support » Fixing WordPress » Simple get_term_by question

  • Resolved Viktor Nagornyy

    (@viktorix)


    Hey,

    I’m using
    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );?>

    to get my taxonomy, but I’m having issues displaying $term_name within php tags. Can someone help?

    Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • use $term->name for taxonomy classification and $term->taxonomy for taxonomy label

    To clarify further:

    get_term_by returns an object, object properties/variables are accessed using the object operator ->, so to access the variables of the $term object, as chinmoy has shown above you just need to use the object operator and one of the valid variable names, such as name, slug, term_id or whatever else (depends what you want).

    echo $object; // Invalid, echo can only deal with numeric or string values (not arrays or objects)
    echo $object->name; // Valid if the object has a variable called name
    echo $object->hello; // Valid if the object has a variable called hello

    Quick way to see the properties/variables of an object is to print the data to screen, like so..

    print '<pre>';
    print_r( $your_object_variable );
    print '</pre>';

    Hope that helps.

    Thanks guys! It works great. Now I’ll know. =)

    You’re welcome.. 🙂

    Also, if you just need the term_id, you could use $wp_query->get_queried_object_id()

    Additionally, the complete list of the available variables of term_object are:

    • 'term_id'
    • 'name'
    • 'description'
    • 'slug'
    • 'count'
    • 'parent'
    • 'term_group'
    • 'taxonomy'

    The is filter but I’m pretty sure it is a function call and is defaulted to empty. You should mess with that.

    I didn’t find any documentation on those, and I don’t know where it should go in the codex, so I put it here.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Simple get_term_by question’ is closed to new replies.