Output Hierarchical List of Upcoming Event Categories

Home Forums Calendar Products Events Calendar PRO Output Hierarchical List of Upcoming Event Categories

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1329805
    Ulf
    Participant

    Hi 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
    Womens

    However 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
    —Womens

    I 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.

    #1329806
    Ulf
    Participant

    This reply is private.

    #1331066
    Cliff
    Member

    Hi. Thanks for all the details.

    Why do you have 'hide_empty' => false?

    Your // Loop through the upcoming events with tribe events category taxonomy does a foreach() but doesn’t use $event.

    You should also probably either wrap tribe_events_cat in 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.

    #1335817
    Ulf
    Participant

    Hi 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!

    #1336289
    Cliff
    Member

    Awesome! I’m very glad I was able to help you get across the finish line.

    Have a great rest of your week.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Output Hierarchical List of Upcoming Event Categories’ is closed to new replies.