Support

Account

Home Forums General Issues Taxonomy Relationship Reply To: Taxonomy Relationship

  • Thanks Elliot, your code gave me a good head start, my end code is a bit different but does what i want!

    In the end i changed Years to a Country taxonomy. Basic setup was as follows:
    Custom Post Type: Speakers
    Taxonomy: Country (attached to Speakers)
    ACF Speakers Field Group with 2 fields:
    – speaker_title
    – speaker_photo
    ACF Page Field Group with 1 Flexible Content field with 2 layouts:
    – basic_wysiwyg (1 wysiwyg field)
    – speakers_country_list (1 taxonomy field to choose multiple countries, set to return as Term Object)

    The following code outputs it all with Countries as headings before each list of Speakers within that country:

    <?php 
    /*
    *  Loop through a Flexible Content field and display it's content with different views for different layouts
    */
    while(has_sub_field("flexible_content")): ?>
     
    	<?php if(get_row_layout() == "basic_wysiwyg"): // layout: Basic Wysiwyg ?>
     
    		<div style="border: 1px solid red; margin: 20px; padding: 20px;">
    			<?php the_sub_field("wysiywg"); ?>
    		</div>
     
    	<?php elseif(get_row_layout() == "speakers_country_list"): // layout: Speaker Country List ?> 
    	
    		<div style="border: 1px solid green; margin: 20px; padding: 20px;">
    			<?php
    				$taxterms = get_sub_field("country"); ?>				
    				<?php foreach ($taxterms as $taxterm) : ?>					
    					<?php
    					$args = array(
    						'post_type' => 'speakers',
    						'tax_query' => array(
    							array(
    								'taxonomy' => 'speakers_country',
    								'field' => 'id',
    								'terms' => $taxterm->term_id
    							)
    						)
    					);
    		
    					$myquery = new WP_Query( $args );
    					if($myquery->have_posts()) : ?>
    						<h2>People in <?php echo $taxterm->name; ?></h2>
    						<ul>
    							<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
    								<li style="margin-bottom: 15px;">
    								<?php 
    								if (get_field('speaker_photo')) {
    									$attachment_id = get_field('speaker_photo');
    									$size = "thumbnail";
    									echo wp_get_attachment_image( $attachment_id, $size );
    									echo "<br />";
    								}
    								?>	
    								<strong><?php the_title(); ?></strong><br />
    								<?php the_field("speaker_title"); ?></li>
    							<?php endwhile; ?>
    						</ul>
    					<?php endif; ?>
    					<?php wp_reset_query(); ?>
    				<?php endforeach; ?>
    		</div> 
    	<?php endif; ?> 
    <?php endwhile; ?>