How to hide empty subcategories (WordPress) - wordpress

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>';

Related

Get parent term_id from get_term_children

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;

Custom type - display all taxonomy name and image

I need to display on my single page, each taxonomy name and image.
i have 10 different image for taxonomy : 'brique'
It's ok for the name but i can't display image
For the image
<?php
$tax = 'brique';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('visuel' . $term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try the below code. first, you need to pass taxonomy Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array: you can check here get_terms()
in get_field you have pass second parameter as term_id or $term object
<?php
$tax = 'brique';
$terms = get_terms( array(
'taxonomy' => $tax,
'hide_empty' => false,
) );
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field( 'visuel', $term );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>

Sub Category of Current Category in Wordpress

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

List categories of top level parent of current category - Wordpress

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>';

Query only direct child and sub-terms of a current term archive

I'm using woocommerce on my theme, and it comes with a product_cat term as a default category on the shop product. I need to show on every product_cat archive its direct child and the sub-tems, but not the sub terms child.
I tried:
<?php $thispage = $wp_query->post; wp_list_categories("taxonomy=product_cat&term=". $term->slug."&title_li=&child_of=".$thispage->slug);?>
and it returned all product_cat as ul and the sub-term of the archive im in, but not the archive's children.
I tried:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat');
foreach ( $terms as $term )
$currentID = get_the_ID();
$args=array(
'taxonomy'=>'product_cat',
'term' => $term->slug,
'child_of'=>$currentID
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ): ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li> <?php the_title();?></li>
<?php endwhile; ?>
<?php endif; ?>
and it returned all the the current archive's children and the sub-terms children without the sub-term it self.
So I tried :
$term_id = $terms;
$taxonomy_name = 'product_cat';
$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>';
and it returned nothing.
Got an idea... if this returns only the sub term:
wp_list_categories( array('child_of' => get_queried_object_id(),'taxonomy' => 'product_cat','title_li' => '','depth' => 1, 'show_option_none'=> ''));
maybe i can exacude all of its children in a query args and then call the wp_list_categories. tried:
$terms = get_the_terms( $post->ID, 'product_cat'); foreach ( $terms as $term ) $currentID = get_the_ID(); $args=array( 'taxonomy'=>'product_cat', 'term' => $term->slug, 'exclude '=> array('child_of' => get_queried_object_id()) ); $my_query = new WP_Query( $args );?>
but it returns only the sub-term children, not the the proudct_cat children...
Anyone?
I'm Speechless. anyone have a clue?
*sorry for my poor English
I think you could do something like
<?php
$taxonomy = 'product_cat';
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $product->id, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' .get_queried_object_id() . '&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>

Resources