$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>List sub-taxonomies from current taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

List sub-taxonomies from current taxonomy

matteradmin11PV0评论

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?

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?

Share Improve this question edited Jul 22, 2015 at 15:10 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Jul 22, 2015 at 12:59 Jérémie Le ScoëzecJérémie Le Scoëzec 11 silver badge1 bronze badge 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. – Pieter Goosen 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/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) – Jérémie Le Scoëzec Commented Jul 22, 2015 at 13:45
Add a comment  | 

1 Answer 1

Reset to default 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/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/wordpress-get-taxonomy-hierarchy-including-children/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far