lundi 5 octobre 2015

Looping Through Custom Tax Terms and Displaying All Posts For Each

I'm using the following code to attempt to loop through each of the terms within my custom taxonomy category-film and then output the term name as a heading together with displaying all the custom posts that belong to that term

<?php $terms = get_terms( 'category-film' ); ?>

<?php foreach( $terms as $term ) : ?>

    <?php $posts = new WP_Query( 'post=film&category-film= ' . $term->slug . '&posts_per_page=-1' ); ?>

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

        <h3><?php echo $term->name; ?></h3>
        <p><?php the_title(); ?></p>

        <div class="col-md-6">
            <?php the_field('url'); ?>
        </div>  

    <?php endwhile; endif; ?>

<?php endforeach; ?>

I am finding, however, that my code outputs nothing whatsoever.

If I move <h3><?php echo $term->name; ?></h3> so that it is outside of the loop (but within the foreach loop) it outputs the names of the terms fine.

So I imagine the problem must be something to do with the loop itself?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire