WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get the current category taxonomy term name? (12 posts)

  1. gimperdaniel
    Member
    Posted 3 years ago #

    At this point it's all a big blur in my head about taxonomies... I have a issue and I have googled it for about 5 hours now and no luck...

    it's simple.

    I have a page that display all posts under a post_type's taxonomy

    post_type = book
    taxonomies for book = non-fiction, romance...

    my url looks like:

    http://www.domain.com/book/romance/

    The url above will show all "romance" books

    All I want to do is get the word "romance". So I can right something like... "Books Posted Under Romance".

    How do I do that? How do I get the taxonomy term name of the page i'm in?

  2. Micah Wood
    Member
    Posted 3 years ago #

    Try get_term_by('id', $post->ID, 'taxonomy_name')
    http://codex.wordpress.org/Function_Reference/get_term_by

  3. gimperdaniel
    Member
    Posted 3 years ago #

    I used this.. and it worked:

    $wp_query->query_vars['taxonomy_name'];

  4. bulini (Giuseppe Surace)
    Member
    Posted 3 years ago #

    not sure, it returns the slug? with -

  5. Travis Smith
    Member
    Posted 3 years ago #

    I used the following:

    global $post;
    $terms = get_the_terms($post->id, 'TAXONOMY_NAME');

    print_r reads:

    Array ( [0] => stdClass Object ( [term_id] => 4 [name] => Smyth [slug] => smyth [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => contestants [description] => [parent] => 0 [count] => 4 ) )

    You can then use any of the following:

    $terms[0]->term_id;
    $terms[0]->name;
    $terms[0]->slug;
    $terms[0]->term_group;
    $terms[0]->term_taxonomy_id;
    $terms[0]->taxonomy;
    $terms[0]->description;
    $terms[0]->parent;
    $terms[0]->count;

    For information on get_the_terms: http://codex.wordpress.org/Function_Reference/get_the_terms

  6. Michal Mau
    Member
    Posted 3 years ago #

    I came up with this after inspecting the $wp_query object:

    $term =	$wp_query->queried_object;
    echo '<h1>'.$term->name.'</h1>';

    And it works great for me.

    Hope it helps somebody.

  7. Frankie Jarrett
    Member
    Posted 3 years ago #

    thanks mau! just what i needed...

  8. Prayas Sapkota
    Member
    Posted 3 years ago #

    Thanks Mau.

  9. Michal Mau
    Member
    Posted 3 years ago #

    Happy to help guys!
    You can combine it into one-liner of course:
    echo $wp_query->queried_object->name;

  10. Michal Mau
    Member
    Posted 3 years ago #

    ... or use a native WordPress function:
    echo get_queried_object()->name;
    :)

  11. dan.lee@uk-cpi.com
    Member
    Posted 3 years ago #

    So this displays the taxonomy type on a taxonomy type page but what about displaying the taxonomy type of a post when viewing it in isolation?

    Dan

  12. Michal Mau
    Member
    Posted 3 years ago #

    @Dan I'm not sure I understood your Q but take a look at following functions and their respective examples and let me know if you need to know more.

    wp_get_object_terms - Retrieves the terms associated with the given object(s), in the supplied taxonomies.

    get_object_taxonomies - Returns all of the taxonomy names of a defined object type

Topic Closed

This topic has been closed to new replies.

About this Topic