Created
September 2, 2015 23:52
-
-
Save holisticnetworking/9efec8fe023749e9eaad to your computer and use it in GitHub Desktop.
WordPress. Loop through taxonomy (in this case, "menu") and list all sub-tax terms' posts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// For archives of taxonomies that allow them, this function will organize posts by | |
// the subterm of a term. | |
// var int $term_id: Not necessary for term archives, but for tax archives or other | |
// uses, this is the term you want to work with. | |
// var str $loop: Use this to specify a loop-$loop.php file. Defaults to the post type. | |
// var str $post_type: The correct post type to query for posts. | |
// var str $tax: The taxonomy to use. | |
// | |
*/ | |
function sub_categories( $term_id=null, $loop=null, $post_type='dish', $tax='menu' ) { | |
global $wpdb; | |
$term = empty( $term_id ) ? $get_queried_object() : get_term_by( 'id', $term_id, $tax ); | |
$current_cat = $term->term_id; | |
$loop = !empty( $loop ) ? $loop : $post_type; | |
$tax_terms = get_terms( $tax, array( | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'child_of' => $term->term_id | |
) | |
); | |
// Do Subquery: | |
if ( $tax_terms ) : | |
foreach ( $tax_terms as $tax_term ) { | |
$this->print_sub_posts( $post_type, $tax, $tax_term, $loop ); | |
} // end foreach #tax_terms | |
// No subcats, just do the query: | |
else : | |
$this->print_sub_posts( $post_type, $tax, $term, $loop, false ); | |
endif; | |
} | |
function print_sub_posts( $post_type, $tax, $term, $loop, $title=true ) { | |
global $wpdb; | |
$subtax_query = null; | |
$args = array( | |
'post_type' => $post_type, | |
'tax_query' => array( | |
array( | |
'taxonomy' => $tax, | |
'field' => 'term_id', | |
'terms' => array( $term->term_id ) | |
) | |
), | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'order_by' => 'title', | |
'order' => 'ASC' | |
); | |
$subtax_query = new WP_Query($args); | |
if( $subtax_query->have_posts() ) : | |
if( $title ) : | |
echo sprintf( | |
'<h2 class="%s-header">%s</h2>', | |
$post_type, | |
$term->name | |
); | |
endif; | |
while ( $subtax_query->have_posts() ) : $subtax_query->the_post(); ?> | |
<article id="post-<?php the_ID(); ?>" <?php post_class( 'row' ); ?> itemscope itemtype="http://schema.org/NewsArticle"> | |
<header class="dish-header columns large-3"> | |
<h3 class="dish-name" itemprop="name"><?php the_title(); ?></h3> | |
</header> | |
<section class="dish-description columns large-6" itemprop="description"> | |
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'glc' ) ); ?> | |
</section><!-- .entry-content --> | |
<footer class="dish-footer columns large-3"> | |
<span class="price base">$<?php echo get( 'prices_base_price' ); ?></span> | |
<?php for( $x=1; $x<4; $x++ ) : | |
if( $name = get( 'prices_mod_' . $x . '_name' ) ) : | |
echo '<span class="price modifications">' . $name . ': $' . get( 'prices_mod_' . $x . '_price' ) . '</span>'; | |
endif; | |
endfor; ?> | |
</footer> | |
</article><!-- #post-## --> <?php | |
endwhile; // End the loop. Whew. | |
endif; // if have_posts() | |
wp_reset_query(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment