0

I have a custom taxonomy called book with terms: 'science fiction', 'romantic', 'history'
Each of these Taxonomies have 4 sub-taxonomies, that I need to display as a sub navigation.

I'd like first to retrieve the current taxonomy of my page, and then display a list of these sub taxonomies.

for now I have this:

<?php
    $taxonomy     = 'book';
    $orderby      = 'name';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    $empty        = 0;

    $args = array(
        'taxonomy'     => $taxonomy,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty
    );
?>

<ul class="guide-navigation">
    <?php wp_list_categories( $args ); ?>
</ul>

I need to get the current taxonomy of my page, to inject it in this code to list its sub-taxonomies. How can I do this?

4
  • You need taxonomy term of the current post?
    – Domain
    Commented Jul 22, 2015 at 13:09
  • book is a taxonomy, everything under book taxonomy is called terms. Commented Jul 22, 2015 at 13:16
  • wp_get_post_terms( 0, 'book', array() ); To get current post terms, you can use this function, to read more follow the link codex.wordpress.org/Function_Reference/wp_get_post_terms
    – Domain
    Commented Jul 22, 2015 at 13:26
  • So what I need are the "second level" (child_of?) of terms from the current term of the page (wich is the "first level" of terms in that specific taxonomy) Commented Jul 22, 2015 at 13:45

1 Answer 1

1

This may help you:

<?php

//first get the current term
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

//then set the args for wp_list_categories
 $args = array(
    'child_of' => $current_term->term_id,
    'taxonomy' => $current_term->taxonomy,
    'hide_empty' => 0,
    'hierarchical' => true,
    'depth'  => 1,
    'title_li' => ''
    );
 wp_list_categories( $args );
?>

Source - https://codex.wordpress.org/Function_Reference/get_term_by (You can hard code the "books" taxonomy or you can just get the active taxonomy).

EDITED

You can create a custom function for getting a taxonomy's childrens just like on the below link.

$hierarchy = get_taxonomy_hierarchy( 'book' ); 

/**
 * Recursively get taxonomy hierarchy
 * 
 * @param string $taxonomy
 * @param int $parent - parent term id 
 * @return array
 */
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
  // only 1 taxonomy
  $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;

  // get all direct decendents of the $parent
  $terms = get_terms( $taxonomy, array( 'parent' => $parent ) );

  // prepare a new array.  these are the children of $parent
  // we'll ultimately copy all the $terms into this new array, but only after they
  // find their own children
  $children = array();

  // go through all the direct decendents of $parent, and gather their children
  foreach ( $terms as $term ){
    // recurse to get the direct decendents of "this" term
    $term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );

    // add the term to our new array
    $children[ $term->term_id ] = $term;
  }

  // send the results back to the caller
  return $children;
}

http://www.daggerhart.com/wordpress-get-taxonomy-hierarchy-including-children/

4
  • It doesn't print anything actually. It seems to be in the right direction though, gonna dig that. Thanks for the help. Commented Jul 22, 2015 at 13:43
  • Please add: var_dump($current_term);die; after the first variable definition. In this way you'll see if the current term is good. If not, you should change that function. Don't forget about the other args, some of them may "help" you to have zero results.
    – Rares P.
    Commented Jul 22, 2015 at 13:48
  • this gives me a: bool(false) and then nothing, tried other args but still nothing. Commented Jul 22, 2015 at 14:19
  • I have a better example, I will edit my post.
    – Rares P.
    Commented Jul 22, 2015 at 14:42

Your Answer

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

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