Home › Forums › Calendar Products › Filter Bar › Display only children categories of a specific parent in a custom filter
- This topic has 4 replies, 2 voices, and was last updated 9 years, 1 month ago by Barry.
-
AuthorPosts
-
February 6, 2015 at 12:49 am #940447gonzalosanzaParticipant
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
– SkateI 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!February 6, 2015 at 12:52 am #940448gonzalosanzaParticipantSorry:
– Music
––Rock
–– Pop
–– Disco
– Sport
–– Basket
–– Futboll
–– SkateFebruary 6, 2015 at 9:10 am #940537BarryMemberHi!
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!
February 6, 2015 at 9:18 am #940549gonzalosanzaParticipantThanks 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( ' ', $term->depth * 2 ) . $term->name, 'value' => $term->term_id, 'data' => array( 'slug' => $term->slug ), 'class' => 'tribe-events-category-' . $term->slug ); } } return $flat_list;
February 6, 2015 at 9:21 am #940555BarryMemberAwesome – and thanks for sharing the final solution 🙂
-
AuthorPosts
- The topic ‘Display only children categories of a specific parent in a custom filter’ is closed to new replies.