Skip to content

Instantly share code, notes, and snippets.

@brycejacobson
Created March 4, 2015 17:48
Show Gist options
  • Save brycejacobson/21ee8a0d2165a7e4203c to your computer and use it in GitHub Desktop.
Save brycejacobson/21ee8a0d2165a7e4203c to your computer and use it in GitHub Desktop.
WordPress Loop inside foreach to get posts under custom taxonomy.
<!-- Begin custom tax loop -->
<ul class="accordion" data-accordion>
<?php
//Retrieve custom taxonomy terms using get_terms and the custom post type.
$categories = get_terms('state');
//Iterate through each term
foreach ( $categories as $category ) :
?>
<li class="accordion-navigation">
<a href="#post-<?php echo $category->slug; ?>"><?php echo $category->name; ?></a>
<?php
//Setup the query to retrieve the posts that exist under each term
$posts = get_posts(array(
'post_type' => 'commercial_represent',
'orderby' => 'menu_order',
'order' => 'ASC',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true,
));
// Here's the second, nested foreach loop that cycles through the posts associated with this category
foreach($posts as $post) :
setup_postdata($post); ////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
?>
<div id="post-<?php echo $category->slug; ?>" class="content">
<div class="row">
<div class="medium-4 columns">
<?php if ( get_post_meta( $post->ID, 'commercial_rep_company', true ) ) : ?>
<h2><?php echo get_post_meta( $post->ID, 'commercial_rep_company', true ); ?></h2>
<?php endif; ?>
</div>
<div class="medium-4 columns">
<?php if ( get_post_meta( $post->ID, 'commercial_rep_contact_name', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_contact_name', true ); ?><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_address', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_address', true ); ?><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_address_2', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_address_2', true ); ?><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_city', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_city', true ); ?>,
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_state', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_state', true ); ?>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_zip', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_zip', true ); ?><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_phone', true ) ) : ?>
<?php echo get_post_meta( $post->ID, 'commercial_rep_phone', true ); ?><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_email', true ) ) : ?>
<a href="mailto:<?php echo get_post_meta( $post->ID, 'commercial_rep_email', true ); ?>"><?php echo get_post_meta( $post->ID, 'commercial_rep_email', true ); ?></a><br>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'commercial_rep_website', true ) ) : ?>
<a href="<?php echo get_post_meta( $post->ID, 'commercial_rep_website', true ); ?>" target="_blank">Website</a><br>
<?php endif; ?>
</div>
<div class="medium-4 columns">
<?php echo '<a href="http://maps.google.com/maps?&q=' . urlencode( get_post_meta( $post->ID, 'commercial_rep_address', true ) . " " . get_post_meta( $post->ID, 'commercial_rep_city', true ) . " " . get_post_meta( $post->ID, 'commercial_rep_state', true ) . " " . get_post_meta( $post->ID, 'commercial_rep_zip', true )) . '&amp;z=14" target="_blank">Map It!</a>'; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment