skip to Main Content

I have done all the child category shows in each parent category. Now I want the current or active category not to be displayed when I am on the category page.

$queried_object = get_queried_object();
$child_terms    = get_term_children ( $queried_object->term_id, 'product_cat' );
$main_term     = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;

$terms_arg = get_terms( 'product_cat', array(
    'orderby'       => 'name', 
    'hide_empty'    => false,
    'parent'        => $main_term->term_id,
) );

if( !empty( $terms_arg ) && !is_wp_error( $terms_arg ) ){

    foreach( $terms_arg as $display_term ){
        printf(
            '<div class="cat-list"><h3%s><a href="%s">%s</a></h3></div>',
            ($display_term->term_id == $queried_object->term_id) ? : '',
            esc_url(get_term_link($display_term->term_id)),
            $display_term->name,
        );
    }
}

2

Answers


  1. try this

    foreach( $terms_arg as $display_term ){
    
        if( $display_term->term_id != $queried_object->term_id ) {
            printf(
                '<div class="cat-list"><h3><a href="%s">%s</a></h3></div>',
                esc_url(get_term_link($display_term->term_id)),
                $display_term->name,
            );
        }
    }
    
    Login or Signup to reply.
  2. According to me get_terms() is having the options of exclude under the argument parameter

    Try with below code

    $terms_arg = get_terms( 'product_cat', array(
    'orderby'       => 'name', 
    'hide_empty'    => false,
    'parent'        => $main_term->term_id,
    'exclude'       => $display_term->term_id, //assuming that $display_term->term_id is getting current category id
    ) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search