Home › Forums › Calendar Products › Events Calendar PRO › Output Hierarchical List of Upcoming Event Categories
- This topic has 5 replies, 3 voices, and was last updated 8 years, 8 months ago by
Cliff.
-
AuthorPosts
-
August 2, 2017 at 5:00 pm #1329805
Ulf
ParticipantHi there,
I’m hoping someone can help me out with this. Basically what I am trying to do is create a hierarchical list of all upcoming event categories (which will be used as a menu).
I am already able to output only upcoming event categories with the following code:
<?php // Get all of the upcoming events $events = tribe_get_events( array( 'eventDisplay' => upcoming, 'posts_per_page' => -1, )); // Loop through the upcoming events with tribe events category taxonomy foreach ( $events as $event ) { //Put each upcoming category in an array $cats[] = get_terms( array( 'taxonomy' => tribe_events_cat) ); } //Grab unique categories and put those in an array $cat_results = array_unique($cats); $count = 0; // Loop through the unique array of categories foreach($cat_results as $cat_result){ // Loop through the terms foreach($cat_result as $the_result){ // Output the name for each unique category echo $the_result->name . '<br/>'; } } ?>However this returns a list that does not show any type of category hierarchy
e.g. This is what I get:
Adults
Boys
Camps
Christmas
Girls
High School
Kelowna, British Columbia
Mens
Pond Hockey
Quebec City, Quebec
Tournaments
WomensHowever I would like to output a hierarchical list like so:
Camps
—Adults
—Christmas
Tournaments
—Boys
——Quebec City, Quebec
—Girls
—High School
——Kelowna, British Columbia
—Mens
—Pond Hockey
—WomensI believe that I am already headed in the right direction, if you have a look at the following code, I am able to break the list up by the parent categories. However the sub-categories listed below the parent are not displaying ONLY subcategories with upcoming events – they are displaying ALL subcategories.
<?php $parentCategories = get_terms(array( 'taxonomy' => tribe_events_cat, 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) ); foreach($parentCategories as $pcat){ echo '<strong>' . $pcat->name . '</strong><br/>'; // GET ALL EVENTS THAT ARE UNDER THE PARENT CATEGORY $leEvents = tribe_get_events( array( 'eventDisplay' => upcoming, 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'tribe_events_cat', 'field' => 'slug', 'terms' => $pcat->slug )) )); foreach($leEvents as $leEvent){ $leagueCategories = get_terms( array( 'taxonomy' => tribe_events_cat, 'parent' => $pcat->term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); } foreach($leagueCategories as $lecat){ echo $lecat->name . '<br/>'; // GET ALL EVENTS THAT ARE UNDER THE PARENT CATEGORY $locEvents = tribe_get_events( array( 'eventDisplay' => upcoming, 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'tribe_events_cat', 'field' => 'slug', 'terms' => $lecat->slug )) )); foreach($locEvents as $locEvent){ $locationCategories = get_terms( array( 'taxonomy' => tribe_events_cat, 'parent' => $lecat->term_id, 'orderby' => 'slug', 'hide_empty' => false ) ); } foreach($locationCategories as $locat){ echo $locat->name . '<br/>'; } } }I am aware that you are limited in the amount of customization you can help out with, that being said if you have any input at all as to why this may not be working as expected – that would be very much appreciated.
Thank you.August 2, 2017 at 5:05 pm #1329806Ulf
ParticipantThis reply is private.
August 4, 2017 at 6:35 pm #1331066Cliff
MemberHi. Thanks for all the details.
Why do you have
'hide_empty' => false?Your
// Loop through the upcoming events with tribe events category taxonomydoes a foreach() but doesn’t use $event.You should also probably either wrap
tribe_events_catin quotes or do this:'taxonomy' => Tribe__Events__Main::TAXONOMY,As you know, we can’t do this customization for you… but maybe looking at the source code of the wp_list_categories() function might help you with your logic?
Please let me know if any of this helped.
August 15, 2017 at 4:49 pm #1335817Ulf
ParticipantHi Cliff,
Thank you very much for the reply, that was incredibly helpful. It was definitely the ‘hide_empty’ => false that was giving me the unexpected output. I believe I had originally copy and pasted that from what I found on a support thread and never questioned it.
Thanks again!
August 16, 2017 at 12:43 pm #1336289Cliff
MemberAwesome! I’m very glad I was able to help you get across the finish line.
Have a great rest of your week.
-
AuthorPosts
- The topic ‘Output Hierarchical List of Upcoming Event Categories’ is closed to new replies.
