delarge
9/8/2016 - 2:46 PM

Loop through taxonomy terms and list posts with that term

Loop through taxonomy terms and list posts with that term

<?php
 $post_type = 'person';

 // Get all the taxonomies for this post type
 $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

 foreach( $taxonomies as $taxonomy ) :

     // Gets every "category" (term) in this taxonomy to get the respective posts
     $terms = get_terms( $taxonomy );

     foreach( $terms as $term ) :

        ?>


          <h1><?php echo $term->name; ?></h1>
          <hr />

          <?php

         $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1" );

         if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();

              ?>

              <div class="person">

                <?php $image = wp_get_attachment_image_src(get_field('person_image'), 'thumbnail'); ?>
              <img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />

                <h2><?php the_title(); ?></h2>

                <?php if (get_field('person_position')): ?>
                  <div class="person__position">
                    <p><?php the_field('person_position'); ?></p>
                  </div>
                  <!-- person__position -->
                <?php endif; ?>


                <?php if (get_field('person_bio')): ?>
                  <div class="person__bio">
                    <?php the_field('person_bio'); ?>
                  </div>
                  <!-- person__bio -->
                <?php endif; ?>


                <?php if (get_field('person_email')): ?>
                  <div class="person__email">
                  <p><?php the_field('person_email'); ?></p>
                  </div>
                  <!-- person__email -->
                <?php endif; ?>


                <?php if (get_field('person_phone')): ?>
                  <div class="person__phone">
                    <p><?php the_field('person_phone'); ?></p>
                  </div>
                  <!-- person__phone -->
                <?php endif; ?>





              </div>
              <!-- person -->

              <?php
         endwhile; endif;

     endforeach;

 endforeach;
?>