Get current page’s taxonomy

I’ve got some pages with a custom taxonomy for each page and i’m trying to retrieve this taxonomy on the page. I’d basically need something like the_current_taxonomy() like the_title(). This has to run outside the loop cos i’ll use it in a custom WP_Query right after.

Edit: Found a solution using a different way to retrieve the information i needed. Thanks for your help guys.

Related posts

Leave a Reply

4 comments

  1. Like Rarst, I am confused what you want to output, the taxonomy or the terms of that taxonomy.

    Taxonomy can be output for example if you make a template file with the name of that taxonomy: taxonomy-name.php

    the title of the taxonomy then becomes:

    <h1 class="page-title"><<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h1>
    

    The terms within a taxonomy can be output with a tagcloud, see http://codex.wordpress.org/Function_Reference/wp_tag_cloud (but that is within the loop)

  2. I assume taxonomy is the same and what you need are terms in that taxonomy, assigned to the post?

    Low level function for this is wp_get_object_terms(). It does need object’s ID so if you are resetting post data then you will need to store that from earlier or dig out of original query ($wp_query->get_queried_object_id() as far as I remember).

  3. If you’re already on the category/term page you can call get_queried_object(); before running your custom query to get category/term info, including slug.

  4. So, i needed to extract the term of a know taxonomy given to a page (like this:

    function register_prod_categoria() {
      register_taxonomy(
         'prod-categoria',
         array( 'produtos', 'page' ),
    

    (produtos being a custom post type, just for info.)).

    I tried various things, among them, this: get_terms('prod-categoria','') This, works, but gives me everything about my taxonomy prod-categoria, which isn’t what i needed.

    Then as explained in the codex, two (interesting for me) parameters are available for get_terms(): child_of and parent. This sounded perfect, so i went: get_terms('prod-categoria','child_of=marca'); and also get_terms('prod-categoria','parent=marca'), marca being a term parent (from the custom taxonomy prod-categoria) from which i wanted to extract the child terms. Both gave me no results. I also tried with the name Marca and the slug marca, nothing.

    The solution i ended up with is this: $tt = the_title('','',false);. I’m getting the page title to use it as a parameter after: $posts = posts_search ('produtos',array('prod-categoria'=>$tt,'prod-cols'=>'5-C-P-F-NF-P')); if($posts) { echo "<table class="table-marca">"; foreach($posts as $post) { ... }

    Btw, the function posts_search() (found it on http://wordpress.stackexchange.com or http://stackoverflow.com if i’m right, don’t remember for sure) allows to do queries on multiple taxonomies.

    I agree very much that it’s far away from being perfect, the title of the page having to be equal to the taxonomy name, but in my case it works. Thanks a lot to Rarst and Piet for trying to help.