Display only children categories of a specific parent in a custom filter

Home Forums Calendar Products Filter Bar Display only children categories of a specific parent in a custom filter

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #940447
    gonzalosanza
    Participant

    First of all, great job TEC team!

    I have created a custom filter for filter bar, based on categories filter.
    This code (which I get from the-events-calendar-filterbar/lib/filters/TribeEventsFilter_Category.php), displays all categories and subcategories.

    		foreach ( $term_items as $term ) {
    			
    			$flat_list[] = array(
    				'name'  => str_repeat( ' ', $term->depth * 2 ) . $term->name,
    				'value' => $term->term_id,
    				'data'  => array( 'slug' => $term->slug ),
    				'class' => 'tribe-events-category-' . $term->slug
    			);
    
    			if ( ! empty( $term->children ) ) {
    				$child_items = $this->flattened_term_list( $term->children, $existing_list );
    				$flat_list = array_merge( $flat_list, $child_items );
    			}
    		}
    
    		return $flat_list;
    

    But I want to display only the subcategories from a specific category name.
    For example: I have
    – Music
    – Rock
    – Pop
    – Disco
    – Sport
    – Basket
    – Futboll
    – Skate

    I want to display subcategories when the category name is ‘music’ (so, just display rock, pop and disco)

    I have tried with this thread https://theeventscalendar.com/support/forums/topic/how-to-display-the-child-category-of-a-specific-parent-category/
    but not result.

    My idea is to use this code, replacing parent_category_name for the appropriated sentence.

    
    	foreach ( $term_items as $term ) {
    		if ( parent_category_name == 'Music' ){
    			$flat_list[] = array(
    				'name'  => str_repeat( ' ', $term->depth * 2 ) . $term->name,
    				'value' => $term->term_id,
    				'data'  => array( 'slug' => $term->slug ),
    				'class' => 'tribe-events-category-' . $term->slug
    			);
    
    			if ( ! empty( $term->children ) ) {
    				$child_items = $this->flattened_term_list( $term->children, $existing_list );
    				$flat_list = array_merge( $flat_list, $child_items );
    			
    			}
    		}
    	}
    	return $flat_list;
    

    Any idea?
    Really thanks!

    #940448
    gonzalosanza
    Participant

    Sorry:
    – Music
    ––Rock
    –– Pop
    –– Disco
    – Sport
    –– Basket
    –– Futboll
    –– Skate

    #940537
    Barry
    Member

    Hi!

    It’s always great to see customizations like this being built – but please do note the amount of support we can provide for this level of customization is, regrettably, pretty limited.

    Can I ask what the primary roadblock is here? Is it determining what the current category is (if there is one)? In other words, is the remaining problem one of swapping out the following line for something dynamic where the parent category isn’t hardcoded?

    if ( parent_category_name == 'Music' )</code>

    Thanks!

    #940549
    gonzalosanza
    Participant

    Thanks for responding so quickly. the problem is solved, a friend gave me the solution. If this helps someone:

    
    	foreach ( $term_items as $term ) {
    
    			if ( (! empty( $term->children ))&& ( $term->name == 'Music' )) {
    				$child_items = $this->flattened_term_list( $term->children, $existing_list );
    				$flat_list = array_merge( $flat_list, $child_items );
    			
    			}else if((empty( $term->children ))){
    				$flat_list[] = array(
    				'name'  => str_repeat( '&nbsp;', $term->depth * 2 ) . $term->name,
    				'value' => $term->term_id,
    				'data'  => array( 'slug' => $term->slug ),
    				'class' => 'tribe-events-category-' . $term->slug
    			);
    			}
    		
    	}
    	return $flat_list;
    
    #940555
    Barry
    Member

    Awesome – and thanks for sharing the final solution 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Display only children categories of a specific parent in a custom filter’ is closed to new replies.