Skip to content

Instantly share code, notes, and snippets.

@ashleycam3ron
Last active July 4, 2018 15:19
Show Gist options
  • Save ashleycam3ron/983db3d607abd9a1692020391a1d1a73 to your computer and use it in GitHub Desktop.
Save ashleycam3ron/983db3d607abd9a1692020391a1d1a73 to your computer and use it in GitHub Desktop.
Loop through CPT and Display Taxonomy Terms as heading if posts exist and CPT posts within
<?php
$tax_args = array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => true,
);
$terms = get_terms('vendors', $tax_args);
foreach($terms as $term){
$term_slug = $term->slug;
$tax_post_args = array(
'post_type' => 'promotion',
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'vendors',
'field' => 'slug',
'terms' => $term_slug
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
if($tax_post_qry->have_posts()) :
$count = 1; ?>
<h2 id="<?php echo $term->slug; ?>"><?php echo $term->name; ?></h2>
<?php while($tax_post_qry->have_posts()) : $tax_post_qry->the_post(); ?>
<article class="col-sm-6">
<h4>Promotion <?php echo $count++; ?></h4>
<?php get_template_part( 'template-parts/content-promos' ); ?>
</article>
<?php endwhile;
else :
//"No posts";
endif;
} //end foreach loop ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment