admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 1This 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/
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
-
bookis a taxonomy, everything underbooktaxonomy 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
1 Answer
Reset to default 1This 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/
本文标签: List subtaxonomies from current taxonomy
版权声明:本文标题:List sub-taxonomies from current taxonomy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749234107a2336988.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


bookis a taxonomy, everything underbooktaxonomy is called terms. – Pieter Goosen Commented Jul 22, 2015 at 13:16