List posts by the terms for a custom taxonomy of any post type


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // List posts by the terms for a custom taxonomy of any post type
  4.  
  5. $post_type = 'posttypename';
  6. $tax = 'taxonomyname';
  7. $tax_terms = get_terms( $tax );
  8. if ($tax_terms) {
  9. foreach ($tax_terms as $tax_term) {
  10. $args = array(
  11. 'post_type' => $post_type,
  12. "$tax" => $tax_term->slug,
  13. 'post_status' => 'publish',
  14. 'orderby' => 'title',
  15. 'order' => 'ASC',
  16. 'posts_per_page' => -1,
  17. 'caller_get_posts'=> 1
  18. );
  19.  
  20. $my_query = null;
  21. $my_query = new WP_Query($args);
  22.  
  23. if( $my_query->have_posts() ) : ?>
  24.  
  25. <h2 class="produktgrupp"><?php /*?>All <?php echo $tax; ?> Posts For <?php */?><?php echo $tax_term->name; ?></h2>
  26. <ul class="produktlistning">
  27. <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
  28.  
  29. <li id="post-<?php the_ID(); ?>">
  30. <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
  31. <span><?php the_post_thumbnail('produktlistning-thumbnail'); ?></span>
  32. <h3><?php the_title(); ?></h3>
  33. </a>
  34.  
  35. </li>
  36.  
  37. <?php endwhile; // end of loop ?>
  38.  
  39. </ul>
  40.  
  41. <?php else : ?>
  42. <?php endif; // if have_posts()
  43. wp_reset_query();
  44.  
  45. } // end foreach #tax_terms
  46. }
  47. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.