WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Simple get_term_by question (7 posts)

  1. Viktor Nagornyy
    Member
    Posted 4 years ago #

    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.

  2. chinmoy
    Member
    Posted 4 years ago #

    use $term->name for taxonomy classification and $term->taxonomy for taxonomy label

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  4. Viktor Nagornyy
    Member
    Posted 4 years ago #

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

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're welcome.. :)

  6. WraithKenny
    Member
    Posted 4 years ago #

    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.

  7. WraithKenny
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic