I'm trying to make a list of all children (subcategories) of a taxonomy category.
Found this function (get_term_children) on developer
developer wordpress
that does what I want, but I have to manually input the parents id.
<?php
$term_id = 10;
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
How can I dynamically get the parent id of the current taxonomy page?
Cheers,
Dennis
Searched stackoverlow, and tried a few methodes, but could not get it to work.
you can use get_queried_object()
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$parent = $queried_object->parent;
Related
I need to list the taxonomies that apply to this post. If the taxonomy only one then this code works well:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
if($terms){
$output = array();
foreach($terms as $term){
$outputnk[] = get_term_link( $term->slug, 'gintype');
$outputme[] = $term->name;
}
}
But if a post has several taxonomy items this code does not work.
This code is working for me:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
foreach($terms as $term){
$term_link = get_term_link( $term );
echo '<div>' . $term->name . '';
}
I am currently using the code below in my category.php file to showe a list of the subcategories of the current category.
<?php
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' .
$term->name . '</a></li>';
}
echo '</ul>';
?>
This is working fine for this application however I would like to amend it now to only show the next level down. Only the direct subcategories for the current category.
Thanks
Richard
Welcome to stackoverflow Richard,
You can get direct child categories with below code
$cat = get_query_var('cat');
$child_categories= get_categories('hide_empty=0&parent='.$cat);
Thanks
I am using the code below which show empty subcategories also which don't have any post in it and I want to hide these. Let me if it is possible and if yes then how
<?php
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name.' ('. $term->count. ')</li>';
}
echo '</ul>';
?>
Thanks a lot in advance
You may use $term->count to check if it have any post.
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
if($term->count > 0){
echo '<li>' . $term->name.' ('. $term->count. ')</li>';
}
}
I have a other solution
$termchildren = get_terms( $taxonomy_name, array( 'hide_empty' => true, 'parent' => $term_id ) );
You can use term count to check empty category
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
if( $term->count > 0 ) {
echo '<li>' . $term->name.' ('. $term->count. ')</li>';
}
}
echo '</ul>';
I'm trying to list the categories of the top level parent of a current category in Wordpres. This is the code I have the at the moment:
$currentcat = get_query_var('cat');
$cats_str = get_category_parents($cat, false, '%#%');
$cats_array = explode('%#%', $cats_str);
$cat_depth = sizeof($cats_array)-2;
<?php wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=0&child_of='.$currentcat); ?>
However this only list the categories of the current category, I want to show all categories underneath the top level of the current category. Thanks
try this:
$term = $wp_query->queried_object;
$term_id = $term->term_id;
$taxonomy_name = 'your_taxonomy';
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>
use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>