0

I've created a query working fine pulling posts from a specific taxonomy—but before I had only 7 taxonomies that were permanent, now I have sub taxonomies under that main taxonomy and they will be dynamic.

Any ideas of how I might be able to write this but have a for-each loop going through this query for any taxonomy that is a child of the main taxonomy (in this example the parent taxonomy is 'Cameras')

So again: I have 7 parent taxonomies ('Cameras' is one) and each one has a dynamic amount of sub taxonomies, i'm trying to create a for each loop that will allow me to do the following without writing the query for each specific sub-taxonomy.

Thanks

<h2>Cameras</h2>
<ul>
<?  $args = array(
                                'post_type'=> 'rental_gear',
                                'type'    => 'cameras',
                                'order'    => 'ASC',
                                'posts_per_page' => '-1'
                                );              

                            $the_query = new WP_Query( $args );
                            if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 

                            ?>
                            <li class="equip-li"><a href="<? the_permalink(); ?>">&rsaquo; <? the_title(); ?></a></li>  
                            <?php endwhile; endif; wp_reset_postdata(); ?>

1

Use get_terms() with child_of (accepts numerical ID of parent term) argument to retrieve child terms and then loop through them and on each iteration use current term in the loop.

Code example (not tested):

$child_terms = get_terms( array( 'child_of' => $cameras_term_id ) );

foreach( $child_terms as $term ) {

    $args = array(
        'post_type'=> 'rental_gear',
        'type'    => $term->term_slug,
        'order'    => 'ASC',
        'posts_per_page' => '-1'
    );              

    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); 
        ?><li class="equip-li"><a href="<?php the_permalink(); ?>">&rsaquo; <?php the_title(); ?></a></li>  
    <?php endwhile; endif; wp_reset_postdata(); ?>

}
  • Are you saying to use get_terms() in the query loop? I need to do the same markup as you see here repeatedly, I need to list the permalink / title in an <li> for each post within that child_of term – RooWM Oct 16 '12 at 18:35
  • @RooWM no, outside of it. Then each iteration of will have same loop with changing child term (if I understood right what you want to achieve). – Rarst Oct 16 '12 at 18:37
  • I think I see what you're saying—do you mind showing me? I'm not sure how to incorporate the current term once it is found – RooWM Oct 16 '12 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.