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
Related
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;
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>';
can anyone explain me how to show the category title with the number of product in that category in category archive page like category 1(10)
the url is http://www.example.com/product-category/category-1/
Can anyone help me..............
I have tried this but it display total product of the website. I want only the product in that category
<h1><?php woocommerce_page_title();
global $woocommerce_loop;
do_action( 'woocommerce_before_subcategory', $category );
echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . $category->count . ')</mark>', $category );
?>
</h1>
I am able to solve this
<h1><?php woocommerce_page_title();
global $wp_query;
// get the query object
$cat_obj = $wp_query->get_queried_object();
if($cat_obj) {
$category_name = $cat_obj->name;
$category_desc = $cat_obj->description;
$category_ID = $cat_obj->term_id;
}
$term = get_term( $category_ID, 'product_cat' );
echo '('. $term->count . ')';
?>
</h1>
Placed this in "archive-product.php"
To list all the categories along with the product count:
$terms = get_terms( 'product_cat' );
foreach( $terms as $term )
{
echo 'Product Category: '
. $term->name
. ' - Count: '
. $term->count;
}
If you have a category ID:
$term = get_term( CAT_ID, 'product_cat' ); //Replace your category ID here
echo 'Product Category: '
. $term->name
. ' - Count: '
. $term->count;
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>';
}
}
}
} ?>